Featured post
jquery - No data when attempting to get JSONP data from cross domain PHP script -
i trying pull latitude , longitude values server on different domain using singe id string. not familiar jquery, seems easiest way go getting around same origin problem. unable use iframes , cannot install php on server running javascript, forcing hand on this.
my queries appear going through properly, not getting results back. hoping here might have idea help, seeing wouldn't recognize obvious errors here.
my javascript function is:
var surl = "http://...omitted.../pull.php"; var idnum = 5a; //in practice 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 javascript run, before, between , after alerts displayed. called , other jsonpcallback alerts not shown.
is there way tell if jsoncallback function has been called?
below php code have running on second server. added count table database can tell when script run. every time call javascript, count has had item inserted , id number 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 * %s loc_id='%s'", $loctable, mysql_real_escape_string($_post['id'])); } if (isset($_get['id'])){ $sql = sprintf("select * %s 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 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 not entirely sure if jsonp returned php correct. when go directly php script without including parameters, following.
({"lat":404,"lon":404})
the callback function not included, can expected when isn't included in original call.
does have idea might going wrong here?
thanks in advance!
change part of code
jsonp: "jsonpcallback",
with:
jsonpcallback : "jsonpcallback",
- Get link
- X
- Other Apps
Comments
Post a Comment