外部域未通过 ajax 请求传递值

External domain is not passing values through ajax request

我使用 CPanel 服务器,我的移动应用程序的配置文件中有域名,但是当我上传索引和所有组件(Javascripts、PHP 文件、SCSS , CSS) 到服务器和 Phonegap 并将其下载到我的手机上它不会检索 MySQL tables 的数据。它有时会在本地机器和移动设备上显示企业的下拉列表,但不会显示存储在第二个 table 中的标记。我想我需要将插件添加到配置文件,但我不确定是哪些插件。我还需要让 AJAX 或 JSON 正常工作。目前我添加了 access origin = "my domain name" 但它仍然不想完全从服务器检索信息。它检索所有其他组件,但这个 PHP 似乎缺少它的功能。 (PS。该应用程序在从服务器打开但在本地计算机上打开时运行良好)。这是我的代码:

AJAX + JavaScript

        <div id="Manu">
        <div class="Cheltenham">
            <select id="Cheltenham" class="select" data-theme="d"></select>
            </div>

            <!--Map-->
            <div id="myMap">
<script>src="http://maps.googleapis.com/maps/api/js"></script>      
            <script type="text/javascript">
/* call the php that has the php array which is json_encoded */
        $.ajax({

                    url: 'http://ct5006-14h.studentsites.glos.ac.uk/FinalProject/cheltenham.php',
                    type: 'GET',
                    dataType: "jsonp",
                    jsonp: 'jsoncallback',
                    crossDomain: true,
                    success: function (data, status) {
                        alert("ajax success");
                        /* call the php that has the php array which is json_encoded */
                        //$.getJSON('', function(data){
                        /* data will hold the php array as a javascript object */
                    $.each(data, function(key, val){
                        var x = document.getElementById("Cheltenham");
                        var option = document.createElement("option");
                        option.text = val.BUSTYPE;
                        option.value = val.REFERENCE;
                        x.add(option);

                    });
                //});
                    },
                    error: function (request, status, error) {
                        alert(status);
                    }
                });


            </script>

            <script src="https://maps.googleapis.com/maps/api/js"></script>

            <script>
                $(document).on('click', '#Cheltenham', function() {
                        var x = document.getElementById("Cheltenham");
                        var referenceID = x.options[x.selectedIndex].value;
                        console.log("referenceID:" + referenceID);
                        console.log("test:" + referenceID);

                        var mapCanvas = document.getElementById('myMap');
                        var mapOptions = {
                            center: new google.maps.LatLng(51.8979988098144,-2.0838599205017),
                            zoom: 12,
                            mapTypeId: google.maps.MapTypeId.ROADMAP
                        }
                        var map = new google.maps.Map(mapCanvas, mapOptions);

                        $.getJSON('SearchBusinesses.php?REFERENCE='+referenceID, function(data){

                        /* data will hold the php array as a javascript object */
                            /* data will hold the php array as a javascript object */
                            $.each(data, function(key, val){

                                var v_lati = val.LATITUDE;
                                var v_longi = val.LONGITUDE;

                                var myLatlng = new google.maps.LatLng(v_lati,v_longi);

                                console.log("LatLongNew:" + myLatlng);

                                var marker = new google.maps.Marker({
                                    map: map,
                                    icon: 'images/marker.png',
                                    position: myLatlng,
                                    animation: google.maps.Animation.DROP,
                                    draggable: false,
                                }); 

                                var contentString = '' + val.NAME;

                                var infowindow = new google.maps.InfoWindow({
                                    content: contentString
                                });

                                google.maps.event.addListener(marker, 'click', function() {
                                    infowindow.open(map, marker);
                                });

                            });

                        });

                        console.log("test1:" + referenceID);



                        google.maps.event.trigger(map, 'resize');
                });

            </script>

            <script>

                function initialize() {
                    var mapCanvas = document.getElementById('myMap');
                    var mapOptions = {
                        center: new google.maps.LatLng(51.8979988098144,-2.0838599205017),
                        zoom: 12,
                        mapTypeId: google.maps.MapTypeId.ROADMAP
                    }
                    var map = new google.maps.Map(mapCanvas, mapOptions)
                }
                google.maps.event.addDomListener(window, 'load', initialize);
            </script>

            </div>    
        </div>

cheltenham.php

<?php


$servername = "localhost";
$username = "...";
$password = "...";
$dbname = "...";

function getEventList(){ 

global $servername, $username, $password, $dbname;

// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
    die("Connection failed: " . $conn->connect_error);
}

$sql = "SELECT * FROM `BUSINESS_TYPE`";
$result = $conn->query($sql);
$rows = array();

if ($result->num_rows > 0) {
    // output data of each row
    while($row = $result->fetch_assoc()) {
        $rows[] = $row;
    }
} else {
    //echo "0 results" . "<br>";
}

echo $_GET ['jsoncallback'].'('. json_encode($rows).');';
    $conn->close();
}

getEventList();

?>

SearchBusinesses.php

<?php

$servername = "...";
$username = "...";
$password = "...";
$dbname = "...";

$evReference = $_GET['REFERENCE'];

function getEventOne(){ 

global $servername, $username, $password, $dbname, $evReference;

// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
    die("Connection failed: " . $conn->connect_error);
}

$sql = "SELECT `LATITUDE`, `LONGITUDE`, `NAME` FROM `BUSINESSES` where `REFERENCE` = '" . $evReference . "'";
$result = $conn->query($sql);
$rows = array();

if ($result->num_rows > 0) {
    // output data of each row
    while($row = $result->fetch_assoc()) {
        $rows[] = $row;
    }
} else {
    //echo "0 results" . "<br>";
}
echo json_encode($rows);
$conn->close();
}

getEventOne();

?>

我使用了 JSON 并在 php 中添加了一行以强制 JSON 传递值

$.getJSON('http://...', function(data){
                    /* data will hold the php array as a javascript object */
                    $.each(data, function(key, val){
                        var x = document.getElementById("Cheltenham");
                        var option = document.createElement("option");
                        option.text = val.BUSTYPE;
                        option.value = val.REFERENCE;
                        x.add(option);
                    });
                });

在我的 chelteham.php 中,我添加了这个并且它起作用了:

     echo json_encode($rows, JSON_FORCE_OBJECT);