我相信,如果您使用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