Get HTTP headers with JavaScript
Can I evaluate the status of a URL on a remote server with JavaScript? Is it c开发者_如何学运维onsidered a cross-domain reference even if I don't get the actual contents of a document?
If it's not possible through plain JavaScript, could I perhaps load the document in an iframe?
What I would like to do is to check if the HTTP status code is 200 and if the Content-Type is text/xml, to make sure the URL a user entered is valid.
I'm using YUI btw.
Can I evaluate the status of a URL on a remote server with JavaScript?
Not in a standard browser security context (which I'll assume unless you say otherwise as it covers almost all use cases).
Is it considered a cross-domain reference even if I don't get the actual contents of a document?
Yes.
If it's not possible through plain JavaScript, could I perhaps load the document in an iframe?
No, and even if you could you wouldn't have access to the HTTP headers.
You're right in your assumptions that any XMLHTTPRequest to a non-local path is considered cross-domain, regardless of its nature.
That's not to say it can't be done. For example, you could route your request through a local server-side script (ie. PHP/Python/Ruby et al) that connects to a remote host, retrieves data and outputs it. It may then be accessed by JavaScript, effectively allowing you to make a cross-domain request with AJAX.
精彩评论