我相信,如果您使用JSONP,则需要在$ .ajax请求中指定回调,然后在REST文件返回中再次指定回调。下面是我一直使用的例子(尽管我不确定,但我确定)。
$.ajax({
url: 'www.domain.com/string/to/your/REST/api',
data: {
dataToBeSent: variable,
dataToBeSent: sessionStorage.getItem('local/session Storage'),
dataToBeSend: "or a string"
},
dataType: 'jsonp',
jsonp: 'jsoncallback',
timeout: 5000,
success: function(data){
alert("Huzzah!");
},
error: function(){
alert("Boohisssss");
}
}); //end ajax call
然后在URL,我会将此代码在文件的底部:
header("Content-type: application/json", true);
echo $_GET['jsoncallback'] . '(' . json_encode($data) . ');';
exit;
在哪里的数据是一个数组,它是JSON编码,在PHP中使用json_encode(),然后用回调函数($ _GET ['jsoncallback'])包装起来。
就像我说的那样,它并不完美,但它一直在为我工作。
任何错误或控制台消息? – 2012-04-20 22:50:10