admin管理员组

文章数量:1330599

I have some javascript code that will make a request to an api. And the request spits out xml as the response. I am using XMLHTTPRequest to get the xml.

In Firebug I notice it just fails, and throws an exception. I am not really sure what going on. Here's a demo in Firebug you'll notice in the console window that it will fail.

Here's the code I run,

xmlHttp = new XMLHttpRequest();
xmlHttp.open( "GET", configgg, false );
xmlHttp.send()

When I was searching for clues online to whats going, there was a topic about issues when requesting stuff from another domain won't work and the issue is server side. If that's the case how would I turn that on(allowing other domains to make xmlhttprequests)? The API is on one of our servers but we are making request tests from other domains to get it to work.

I have some javascript code that will make a request to an api. And the request spits out xml as the response. I am using XMLHTTPRequest to get the xml.

In Firebug I notice it just fails, and throws an exception. I am not really sure what going on. Here's a demo in Firebug you'll notice in the console window that it will fail.

Here's the code I run,

xmlHttp = new XMLHttpRequest();
xmlHttp.open( "GET", configgg, false );
xmlHttp.send()

When I was searching for clues online to whats going, there was a topic about issues when requesting stuff from another domain won't work and the issue is server side. If that's the case how would I turn that on(allowing other domains to make xmlhttprequests)? The API is on one of our servers but we are making request tests from other domains to get it to work.

Share edited Feb 18, 2011 at 14:14 Bombcode asked Feb 3, 2011 at 16:37 BombcodeBombcode 1941 gold badge3 silver badges14 bronze badges 2
  • If you use google chrome for debugging you would have seen this error: XMLHttpRequest cannot load api.internetvideoarchive./Video/…. Origin elvis.rowan.edu is not allowed by Access-Control-Allow-Origin. – gnur Commented Feb 3, 2011 at 16:54
  • @gnur is there a way to allow all domains access? – Bombcode Commented Feb 3, 2011 at 17:38
Add a ment  | 

5 Answers 5

Reset to default 1

As suggested in the other answers the fact that you are trying to access something from another domain is the cause of the script failing.

To fix this you could circumvent the javascript limitation by loading a local PHP script that echo's the contents of the API url.

Same_origin_policy does not allow you to make calls across domain. You can look into CORS in you are working with new browsers or you need to use a proxy or you need to start using JSONP.

Setting up the browser's security to allow cross domain calls is asking for trouble if the user surfs the web with it.

The answer everyone here will love will involve the Same Origin Policy, which disallows cross-domain calls. But I've been through something similar and I know that's not the answer you're looking for.

I know how to do this in Internet Explorer using an MSXML ServerXMLHTTP object, but as far as trying it in other browsers, try doing this:

xmlHttp = new XMLHttpRequest();  
xmlHttp.open( "GET", configgg, false );  
xmlHttp.setRequestHeader("Host", configgg);
xmlHttp.send();

I think the reason it's failing is because when you're using the send method, but not including any arguments, you have to tell it null, like so:

xmlHttp.send(null);

This happens with some other methods, as well (getComputedStyle also fails if you don't include null as the second argument).

    var xmlhttp = new XMLHttpRequest();
    xmlhttp.open('GET','baselead.xml',false);
    xmlhttp.setRequestHeader('Content-Type', 'text/xml');
    xmlhttp.send('');

you have to use 'baselead.xml' qoute('') it works for me in chrome

本文标签: