[英]Receive serialized in php data by using ajax
我有一個php腳本,該腳本返回在php數據中序列化的內容。 我嘗試使用jQuery 1.7中的$ .ajax()方法接收此數據。 這是例子。
$.ajax({
url: 'http://input.name/get.php?do=lookup' + '&domain=twittorama&tlds=.ru,.com,.net,.comf.ru',
type: 'GET',
dataType: 'text',
cache: 'false',
complete: function(res) {
alert('COMPLETE() done');
console.log(res);
}
});
在控制台中,我只能看到
Object { readyState=0, status=0, statusText="error"}
那么,我做錯了什么? 請問你能幫幫我嗎?
UPD
有趣的通知:如果我使用JSONP dataType請求,則可以接收數據,但無法處理它。 這是一個例子 。
$.ajax({
url: 'http://input.name/get.php?do=lookup',
data: 'domain=twittorama&tlds=.ru,.com,.net,.comf.ru',
dataType: 'jsonp',
cache: false,
success: function(data) {
alert("Data: "+data);
},
error: function(jqXHR, textStatus, errorThrown) {
alert("Error: "+textStatus);
console.log(jqXHR);
}
});
代替complete:
使用success:
然后res
將是您的ajax請求返回的數據。
記住要使用error:
如果您調用時發生錯誤,也可能會出現在控制台輸出中,以防萬一。
碼:
$.ajax({
url: 'http://input.name/get.php?do=lookup',
data: 'domain=twittorama&tlds=.ru,.com,.net,.comf.ru',
cache: false,
success: function(data) {
alert("Data: "+data);
},
error: function(jqXHR, textStatus, errorThrown) {
alert("Error: "+textStatus);
console.log(jqXHR);
}
});
您的代碼可能沒問題,但是您試圖違反相同的原始政策 。 基本上,如果您的網站是http://aaa.com/
,則無法將AJAX調用到http://bbb.com/
。
有幾種解決方法:
但是他們大多數都要求雙方都表現得很好。
響應是完整功能的第二個參數:
$.ajax({
url: 'http://input.name/get.php?do=lookup' + '&domain=twittorama&tlds=.ru,.com,.net,.comf.ru',
type: 'GET',
dataType: 'text',
cache: 'false',
complete: function(res,response) {
alert('COMPLETE() done');
console.log(response);
}
});
更多信息: http : //api.jquery.com/jQuery.ajax/
您還應該考慮使用JSON,而不是php序列化數據
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.