-1

In my javascript i have,

$.ajax({
                    type: 'GET',
                    url: 'http://133.333.33.33/reporting/summary_table.php?loc_id='+locid+'&loc_type='+loctype+'',
                    async: false,
                    success: function(data) {
                        alert(data);
                    },
                    dataType: 'json'
                });

In my server side i have this,

$result = mysql_query($query);
$rows = array();
while ($row = mysql_fetch_assoc($result)) {
    $rows[] = $row;
}
echo json_encode($rows);

when I check my firebug on FF, the ajax response is nothing, it fires out an error instead. What did I miss?

Tsukimoto Mitsumasa
  • 541
  • 4
  • 19
  • 42
  • 3
    You cannot make cross-domain Ajax calls unless the server supports and enables CORS. Or you have to use JSONP, which must be supported by the server as well. – Felix Kling Feb 21 '13 at 17:28
  • 2
    "it fires out an error instead" — **WHAT** error? – Quentin Feb 21 '13 at 17:29
  • You are using [an **obsolete** database API](http://stackoverflow.com/q/12859942/19068) and should use a [modern replacement](http://php.net/manual/en/mysqlinfo.api.choosing.php). – Quentin Feb 21 '13 at 17:30
  • Is `133.333.33.33` the same site the JavaScript is on? Why are you using `async: false`? – gen_Eric Feb 21 '13 at 17:32
  • @Quentin, I am now voting to close this question. I really forgot I already asked the same question. My bad. Please vote to close this question. Thanks! – Tsukimoto Mitsumasa Feb 21 '13 at 18:32

1 Answers1

0

Call your service as follows:

$.ajax({
url: 'http://yourserver.com/reporting/summary_table.php?loc_id='+locid+'&loc_type='+loctype+'',
success: function(data) {
alert(data);
},
dataType: 'jsonp'
});

Watch out for the "jsonp" dataType.

Then implement this little change on your server side script:

$jsonp = json_encode($rows);
if(isset($_GET["callback"])) $jsonp = $_GET["callback"]."($jsonp)";
echo $jsonp;

So your php it's gonna be capable of answering jsonp requests.

hyunkeln
  • 429
  • 2
  • 9