2017-03-08 119 views
1

假設我有興趣檢查使用XMLHttpRequest發送的參數。有沒有辦法從XMLHttpRequest響應中獲取參數?

例如,如果我發送POST請求param'option = 1',我可以從響應中檢索嗎?

我檢查了方法和屬性,但沒有看到獲取它的方法。

+0

不,響應是響應而不是請求。但是,您應該能夠簡單地記住您發送的參數,並將它們與響應一起傳遞給檢查器。 – Bergi

+0

你需要提供更多的上下文:XMLHttpRequest在客戶端,你用什麼來在服務器端接收請求?順便說一下,您需要檢查請求中的參數而不是響應。 – Mosd

+0

關鍵是獲取請求中發送的參數,而不必將它們存儲在變量中。 儘管如此,感謝您的答覆。 – Vers

回答

0

正如BERGI說這是不可能的,以檢索參數與響應請求一起發送。所以我正在結束這個問題。

感謝大家幫助!

0

Fire a XMLHTTPRequest並檢查瀏覽器的JS控制檯(F12 for Chrome/Firefox)中的響應對象。

我相信數據不存在,至少我曾經改變過一個項目的XMLHttpRequestopen()方法(當然,我可能只是太傻而無法找到它)。這樣,當向用戶打印錯誤/向錯誤報告後端發送錯誤時,我的默認錯誤處理程序知道原始URL。

粗糙的代碼片段,從項目初始化拉代碼:

/** 
* Check XMLHttpRequest availability 
*/ 
var ajax = null; 
var proto = null; 
if (window.XMLHttpRequest) { 
    ajax = new XMLHttpRequest(); 
    proto = XMLHttpRequest.prototype; 
} else if (window.ActiveXObject) { 
    try { 
     ajax = new ActiveXObject("Msxml2.XMLHTTP.6.0"); 
     proto = ActiveXObject("Msxml2.XMLHTTP.6.0").prototype; 
    } catch (e) { } 
}  

if (ajax == null) { 
    alert ("Can not create AJAX object. You need a more recent browser!"); 
    return; 
}  

/** 
* Update ajax prototype to store the URL (for better error handling) 
*/ 
try { 
    var origOpen = proto.open; 
    proto.open = function (method, url) { 
     this._url = url; 
     return origOpen.apply (this, arguments); 
    }  
} catch (e) { 
    console.log ("Can not patch XMLHttpRequest to store URL. Console output will omit them..."); 
} 

你需要去適應這對於傳遞給send()功能,而不是POST數據。 請注意,該方法可能是不好的風格,我的JS風格可能會更糟!

更好:但你總是可以直接通過POST數據到回調函數,而不將其存儲在了XMLHttpRequest對象:

var postData = "SomeStuff-Foobar123"; 
var ajax = new XMLHttpRequest(); //add magic for other browsers here 
ajax.open ("POST", "ajax.php", true); 
ajax.onreadystatechange = function() { 
    if (this.readyState != 4 || this.status != 200) { 
     console.log ("Not ready, yet..."); 
     return 0; 
    } 
    //response is in this.responseText 
    //but you can still access the parent objects! 
    console.log ("Done with Code 200. POSTed data: " + postData); 
} 
ajax.send (postData); 
相關問題