有没有办法从XMLHttpRequest响应中获取参数?

问题描述:

假设我有兴趣检查使用XMLHttpRequest发送的参数。有没有办法从XMLHttpRequest响应中获取参数?

例如,如果我发送POST请求param'option = 1',我可以从响应中检索吗?

我检查了方法和属性,但没有看到获取它的方法。

+0

不,响应是响应而不是请求。但是,您应该能够简单地记住您发送的参数,并将它们与响应一起传递给检查器。 – Bergi

+0

你需要提供更多的上下文:XMLHttpRequest在客户端,你用什么来在服务器端接收请求?顺便说一下,您需要检查请求中的参数而不是响应。 – Mosd

+0

关键是获取请求中发送的参数,而不必将它们存储在变量中。 尽管如此,感谢您的答复。 – Vers

正如BERGI说这是不可能的,以检索参数与响应请求一起发送。所以我正在结束这个问题。

感谢大家帮助!

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);