[英]Cannot get xmlhttprequest.responseText from JQuery
我是 JQuery 的新手。
我得到了這個功能
function verify_at_bd() {
var u = "foo";
var p = "bar";
return $.post('auth.php', {
name: u,
password: p,
mobile: ''
},
function(result) {
return result;
}, 'json');
}
如果我執行console.log(verify_at_bd())
我得到一個 xmlhttprequest 但無法訪問 responseText 屬性。 我在 PHP 腳本中使用header("Content-Type: application/json")
。
我在 OS X 上使用 Firefox 3.6。
開始:
$.ajax({
type: 'POST',
url: 'auth.php',
data: {name: 'foo', password: 'bar', mobile: ''},
success: function(result, textStatus, xmlhttprequest){
console.log( $.parseJSON(xmlhttprequest.responseText).status);
},
dataType: 'json'
});
就這樣。
嗯,首先,XHR 必須處於 readyState 4 才能獲得 responseText。
其次,在我看來你在濫用 $.post()。 這是一個異步調用,您不處理 $.post() 的結果,而是在您的成功方法 (function(result)) 中處理結果。
如果您確實需要reponseText,則向您的成功函數添加更多參數並在那里捕獲xhr。
請參閱 $.post 的 jquery 文檔以查看哪個 arg 是 xhr。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.