我是JS的新手,并且很难阅读以下JS代码。
该函数的第一个参数是PHP脚本的url,第二个是字符串。
令我困惑的是如何在行后阅读代码: self.xmlHttpReq.open('POST',strURL,true);
此后会发生什么?我应该看看这行代码?剧本? 打开后会发生什么?
function check_detail(strURL, pids)
{
var xmlHttpReq = false;
var self = this;
// Mozilla/Safari
if (window.XMLHttpRequest) {
self.xmlHttpReq = new XMLHttpRequest();
}
// IE
else if (window.ActiveXObject) {
self.xmlHttpReq = new ActiveXObject("Microsoft.XMLHTTP");
}
self.xmlHttpReq.open('POST', strURL, true);
self.xmlHttpReq.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
self.xmlHttpReq.onreadystatechange = function()
{
if (self.xmlHttpReq.readyState == 4)
updatepage(self.xmlHttpReq.responseText, pids);
}
self.xmlHttpReq.send(getquery(pids));
}
答案 0 :(得分:7)
密钥是对“send()”的调用,它实际上启动了HTTP请求。这种情况会发生,但代码会立即进行而无需等待结果。
当服务器响应时,浏览器将调用设置为“readystatechange”处理程序的匿名函数。究竟何时发生这种情况是不可预测的;换句话说,它是异步。
因此,“updatepage()”调用将在“check_detail()”函数返回后很久发生。
答案 1 :(得分:2)
当你发出一个Ajax请求时(这就是你在这里做的)它是异步的,这意味着你不知道它什么时候会返回,所以你不能等待返回。
相反,您需要设置您的函数,以便在请求返回时启动一个函数来处理响应。这是onreadystatechange
篇。
所以年代表将是:首先send()
将发生,它会将getquery()
方法的结果发送到PHP页面。当返回时,onreadystatechange
中定义的函数将触发,它将调用updatepage()
并将从Ajax调用发回的文本以及pids
参数传递给它。 / p>
答案 2 :(得分:0)
如果您是JavaScript的新手,那么我会说这是浪费时间试图弄清楚这里发生了什么 - 您正在学习如何使用XHR对象,如何制作跨浏览器,< em>和你正在同时学习JavaScript。
我建议使用jQuery这样的JavaScript库来执行Ajax - 当你学习JavaScript时,不要试着全部学习它。
其中大部分都可以用以下内容代替:
$.post(strURL, function (data) {
updatePage(data);
});
答案 3 :(得分:0)
这是简单的Ajax函数
function check_detail(strURL, pids)
{
// definning new variable
var xmlHttpReq = false;
// creating variable self which will function as this
var self = this;
// creating HTTP request maker for Mozilla/Safari
if (window.XMLHttpRequest) {
self.xmlHttpReq = new XMLHttpRequest();
}
// creating HTTP request maker in IE
else if (window.ActiveXObject) {
self.xmlHttpReq = new ActiveXObject("Microsoft.XMLHTTP");
}
// so this is the confusing part right ?
// xmlHttpReq.open opens connection tu the strURL and infomation sending method
// will be POST method ( other passible values can be GET method or even else )
self.xmlHttpReq.open('POST', strURL, true);
// this defines HTTP request header (small information about what we are sending)
// in fact this is sending Content-type of information
self.xmlHttpReq.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
// when HTTP request maker state will be changed for example:
// the data will be sent or data will be received this function will be fired
self.xmlHttpReq.onreadystatechange = function()
{
// readyState 4 means data has been received
if (self.xmlHttpReq.readyState == 4)
updatepage(self.xmlHttpReq.responseText, pids); // updatepage is user defined function
}
// this actually sends the HTTP request which is made above
// but don't be confused because of this code ordering
// I mean the function defining what to do when content will be received is implemented
// before sending HTTP request right ?
// thats because if the data is too small and internet is really fast HTTP query can be
// executed faster then defining new function which will cause javascript error
self.xmlHttpReq.send(getquery(pids));
}
希望这会有所帮助
如果不
更多关于ajax:http://en.wikipedia.org/wiki/Ajax_(programming)