Here's a sample XMLHttpRequest I cobbled together from w3schools
下面是我从w3schools收集的XMLHttpRequest示例
Using the XMLHttpRequest object
XMLHttpRequest always returns a zero status.
XMLHttpRequest总是返回零状态。
Nothing shows up in Firefox's error console.
Firefox的错误控制台什么都没有显示。
If I change the request to synchronous one by changing the line
如果我通过更改行将请求更改为同步请求
xmlhttp.open("GET","SBL_PROBES.htm",true);
to
来
xmlhttp.open("GET","SBL_PROBES.htm",false);
and un-comment the line
和取消一行的注释
//T = xmlhttp.responseText;
The text of the requested file is returned.
返回所请求文件的文本。
The HTM and the file reside in the same directory. If you try this you will need a file SBL_PROBES.htm there also, it's contents are irrelevant.
HTM和文件位于同一个目录中。如果您尝试这样做,您将需要一个文件SBL_PROBES。htm,它的内容是无关的。
I'm using Firefox 3.6.22.
我使用Firefox 3.6.22。
Could this be a cross domain problem? If so, why does it work as a synchronous request?
这是一个跨域问题吗?如果是,为什么它作为一个同步请求工作?
15
You can use a function inside the if statement. This function is executed when readystate changes to 4.
可以在if语句中使用函数。当readystate更改为4时,将执行此函数。
var handleRespOnse= function (status, response) {
alert(response)
}
var handleStateChange = function () {
switch (xmlhttp.readyState) {
case 0 : // UNINITIALIZED
case 1 : // LOADING
case 2 : // LOADED
case 3 : // INTERACTIVE
break;
case 4 : // COMPLETED
handleResponse(xmlhttp.status, xmlhttp.responseText);
break;
default: alert("error");
}
}
var xmlhttp=new XMLHttpRequest();
xmlhttp.Onreadystatechange=handleStateChange;
xmlhttp.open("GET","SBL_PROBES.htm",true);
xmlhttp.send(null);
Your old code did a asynchronous call and continued just with the alert Statement. T was empty at this time.
您的旧代码执行了一个异步调用,并继续使用alert语句。这时T是空的。
Ok, I'll explain a little bit how this whole thing works:
好吧,我来解释一下整个过程:
First we define two callback functions, which we call later in the request, named handleResponse and handleStateChange.
首先,我们定义两个回调函数,稍后在请求中调用它们,名为handleResponse和handleStateChange。
Afterwards we create a Object, which represents the XMLHttpRequest
然后我们创建一个对象,它表示XMLHttpRequest
var xmlhttp=new XMLHttpRequest();
This results in an Object as follows (simplyfied):
这就产生了如下的对象(简单化):
XMLHttpRequest { status=0, readyState=0, multipart=false, Onreadystatechange=handleEvent()}
With the open(...) function call you set parameters for the request:
使用open(…)函数调用您为请求设置参数:
xmlhttp.open("GET","SBL_PROBES.htm",true);
This means, do a asynchronous GET Request to fetch the Page SBL_PROBES.htm Then the send(...) function is called which fires the request itself.
这意味着,执行一个异步GET请求来获取SBL_PROBES页面。然后调用send(…)函数来触发请求本身。
We registered a callback function for the onreadystatechange, as you can imagine, this is actually an eventHandler. Each time the state changes this function is called. (It is the same as if you register a callback function to an onKeyUp Event in a form, this callback is triggered each time your key goes up :) )
我们为onreadystatechange注册了一个回调函数,可以想象,这实际上是一个eventHandler。每当状态改变时,就调用这个函数。(这与在表单中向onKeyUp事件注册回调函数是一样的,这个回调会在每次您的键上升时触发:)
The only case which is of interest for your problem is state 4. Therefor the handleRequest callback function is called only in state 4. At this time you Request has actually a result, and further a status. (Status means your webserver returned a status code 200=ok, 404=not found etc.)
唯一对你的问题感兴趣的情况是状态4。因此handleRequest回调函数只在状态4中调用。此时,您的请求实际上具有一个结果,并进一步具有一个状态。(状态意味着您的web服务器返回一个状态码200=ok, 404=not found等)
That is not all the magic which is behind the ajax stuff, but should give you a simplified overview, what is actually happening behind the scenes. It is important that you test this on a webserver, do not use file:// for testing.
这并不是ajax背后的所有神奇之处,但应该给您一个简单的概述,即幕后发生的实际情况。在webserver上测试这一点很重要,不要使用file://用于测试。
If you need more in detail info, just let me know.
如果你需要更多的细节信息,请告诉我。
8
Status Zero happens for two reasons.
状态零发生有两个原因。
I believe you are seeing #2 here. SO you need to cancel the button click.
我相信你在这里看到的是第二条。所以你需要取消按钮点击。
In your code above that alert(T) will always say nothing when the request is asynchronous.
在您上面的代码中,当请求是异步的时候,该警报(T)将始终保持沉默。
3
Its because async returns before the request returns. Synchronous requests return after the request returns.
因为异步在请求返回之前返回。在请求返回后返回同步请求。
Try manipulating your logic in here.
试着在这里操作你的逻辑。
xmlhttp.Onreadystatechange=function()
{
alert ("rdystate: " + xmlhttp.readyState);
alert ("status: " + xmlhttp.status);
alert ("Text: " + xmlhttp.statusText);
if (xmlhttp.readyState==4 && xmlhttp.status==200)
{
T = xmlhttp.responseText;
alert(T);
}
}
1
I've battled the problem of not getting a result when using asynchronous XMLHttpRequest open statement. Since this question is the first I found when using google, here is how I solved it:
我曾遇到过使用异步XMLHttpRequest open语句时得不到结果的问题。由于这个问题是我在使用谷歌时发现的第一个问题,下面是我如何解决它的:
If you use a button that is inside a form, make sure it is set to type="submit" and Onclick="return myFunction()". And in myFunction(), make sure you return false, not true! By returning true from the function, you reload the page and the XML object disappears. If you return false, the XML request gets the time it needs to complete and the onreadystatechange function will be run.
如果使用表单内部的按钮,请确保将其设置为type="submit"和Onclick="return myFunction()"。在myFunction()中,确保返回false,而不是true!通过从函数返回true,重新加载页面,XML对象就消失了。如果返回false,则XML请求获得完成所需的时间,并运行onreadystatechange函数。
Source: Flask Mailing List
来源:烧瓶邮件列表
0
I have now received the good response to this common problem. The response follow:
我现在已经收到了对这个普遍问题的良好回应。响应:
This is a very common problem when developing for the web. There's two ways around it.
在为web开发时,这是一个非常常见的问题。有两种方法。