Cant retrieve remote json data
I have m开发者_开发百科y json data here : I cant call data from my own website to my local site. When I copy the same file to local root, it works. I changed the chmod of the remote file and added some headers but no change. Any ideas?
I guess it is because cross site scripting in JavaScript is forbidden. For remote calls you can use JSONP or create a proxy which simply grabs content using curl.
assumed, you get a response (otherwise it should be an problem of your setting of allow_url_fopen):
What you get there is not regular JSON, your JSON is surrounded by Parentheses.
Furthermore you should provide the JSON utf8-encoded, otherwise you'll get problems, if there are non-ASCII-chars inside.
Guys, I see the problem was I didnt use jsoncallback=? so this is the
js code
$.getJSON('http://geryit.com/_gazel/reviews_json.php?jsoncallback=?',{pid:1},function(data){
if(data.length){
for(i=0;i<data.length;i++){
console.log(data[i].title);
}
}
});
and this is the
php code
include "db.php";
$pid = $_GET["pid"];
$q= mysql_query("select * from reviews where pid = $pid");
$reviews = array();
while($r = mysql_fetch_array($q)) $reviews[] = $r;
$reviews = json_encode($reviews);
echo $_GET["jsoncallback"]."(".$reviews.")";
Thanks for your responses.
精彩评论