问题
url = "http://example.com"
new Ajax.Request(url, {
onComplete: function(transport) {
alert(transport.status);
}
});
I'd like that to return a status of 200 if the site is working, or 500 if it is not working, etc.. But that code is returning 0 all the time.
Ultimately, I want to have a setinterval function that regularly pings a website for uptime status.
回答1:
With JQuery you should get your status with a code similar to what you have, and it will be something like this:
$.ajax({
url: 'some.url.com',
type: 'POST',
complete: function(transport) {
if(transport.status == 200) {
alert('Success');
} else {
alert('Failed');
}
}
});
And if you want to use prototype your code should only add this:
onXYZ:function(transport){
}
In the text above XYZ should be replaced by the http status code you want to catch for the response.
Hope this helps
回答2:
Ajax libraries don't "return status codes" themselves; that code is the HTTP response code returned in the response from the server. A status code of 200 indicates success; 404 indicates "not found", etc.
It's probable that a response code of 0 means the request wasn't even attempted. Is the request URL under the same domain (subdomain, if applicable) as that which the page is coming from? If not, then you may be running into problems with the same-origin policy, which prevents scripts from creating arbitrary requests.
To work around this, you'll need to proxy the data on the server side; for example using a script/framework handler/whatever which executes the web request and passes the data back down to clients. Call the "local" proxy instead of the remote data source.
回答3:
Prototype has onXYZ callbacks, for example:
new Ajax.Request(url, {
method: 'get',
on500: function(transport) {
alert("failed!");
},
onSuccess: function(transport) {
alert("success!");
}
});
On thing though, if the website is down (as in, not reachable), it will not return a 500 error, so your approach isn't very good for starters.
回答4:
I was able to make remote domains work by creating a server side proxy file to pass the status code through. I used the code on this post to create a asp.net page that would just set the status code of the page to the web request status code.
I then used the ajax example that Chermosillo provided like so.
$.ajax({
url: 'URLTestProxy.aspx?url=http://some.url.com',
type: 'POST',
complete: function(transport) {
if(transport.status == 200) {
alert('Success');
} else {
alert('Failed');
}
}
});
This way here you can get around the same origin policy and still get the status code of the remote url.
回答5:
It may be helpful to know that the transport param in the example is an XMLHttpRequest object. Full details of the methods and properties available from this object can be found here:
http://www.w3.org/TR/XMLHttpRequest/#the-xmlhttprequest-interface
Most notable in the context of this question are the response items:
readonly attribute unsigned short status;
readonly attribute DOMString statusText;
DOMString getResponseHeader(DOMString header);
DOMString getAllResponseHeaders();
readonly attribute DOMString responseText;
readonly attribute Document responseXML;
回答6:
You should be able to get any arbitrary header with the getResponseHeader() method.
For jQuery you might try:
complete: function(transport, textstatus){
alert(transport.getResponseHeader("Status"))
}
warning: not tested
来源:https://stackoverflow.com/questions/1140456/can-prototype-or-jquery-return-an-http-status-code-on-an-ajax-request