No data when attempting to get JSONP data from cross domain PHP script
- by Alex
I am trying to pull latitude and longitude values from another server on a different domain using a singe id string. I am not very familiar with JQuery, but it seems to be the easiest way to go about getting around the same origin problem. I am unable to use iframes and I cannot install PHP on the server running this javascript, which is forcing my hand on this.
My queries appear to be going through properly, but I am not getting any results back. I was hoping someone here might have an idea that could help, seeing as I probably wouldn't recognize most obvious errors here.
My javascript function is:
var surl = "http://...omitted.../pull.php";
var idnum = 5a; //in practice this is defined above
alert("BEFORE");
$.ajax({
url: surl,
data: {id: idnum},
dataType: "jsonp",
jsonp : "callback",
jsonp: "jsonpcallback",
success: function (rdata) {
alert(rdata.lat + ", " + rdata.lon);
}
});
alert("BETWEEN");
function jsonpcallback(rtndata) {
alert("CALLED");
alert(rtndata.lat + ", " + rtndata.lon);
}
alert("AFTER");
When my javascript is run, the BEFORE, BETWEEN and AFTER alerts are displayed. The CALLED and other jsonpcallback alerts are not shown.
Is there another way to tell if the jsoncallback function has been called?
Below is the PHP code I have running on the second server. I added the count table to my database just so that I can tell when this script is run. Every time I call the javascript, count has had an extra item inserted and the id number is correct.
<?php
header("content-type: application/json");
if (isset($_GET['id']) || isset($_POST['id'])){
$db_handle = mysql_connect($server, $username, $password);
if (!$db_handle)
{
die('Could not connect: ' . mysql_error());
}
$db_found = mysql_select_db($database, $db_handle);
if ($db_found)
{
if (isset($_POST['id'])){
$SQL = sprintf("SELECT * FROM %s WHERE loc_id='%s'", $loctable, mysql_real_escape_string($_POST['id']));
}
if (isset($_GET['id'])){
$SQL = sprintf("SELECT * FROM %s WHERE loc_id='%s'", $loctable, mysql_real_escape_string($_GET['id']));
}
$result = mysql_query($SQL, $db_handle);
$db_field = mysql_fetch_assoc($result);
$rtnjsonobj -> lat = $db_field["lat"];
$rtnjsonobj -> lon = $db_field["lon"];
if (isset($_POST['id'])){
echo $_POST['jsonpcallback']. '('. json_encode($rtnjsonobj) . ')';
}
if (isset($_GET['id'])){
echo $_GET['jsonpcallback']. '('. json_encode($rtnjsonobj) . ')';
}
$SQL = sprintf("INSERT INTO count (bullshit) VALUES ('%s')", $_GET['id']);
$result = mysql_query($SQL, $db_handle);
$db_field = mysql_fetch_assoc($result);
}
mysql_close($db_handle);
} else {
$rtnjsonobj -> lat = 404;
$rtnjsonobj -> lon = 404;
echo $_GET['jsonpcallback']. '('. json_encode($rtnjsonobj) . ')';
}?>
I am not entirely sure if the jsonp returned by this PHP is correct. When I go directly to the PHP script without including any parameters, I do get the following.
({"lat":404,"lon":404})
The callback function is not included, but that much can be expected when it isn't included in the original call.
Does anyone have any idea what might be going wrong here?
Thanks in advance!