简体   繁体   中英

ajax giving blank response

I have a xml content stored in the location, http://localhost:8080/cleo-primer/rest/elements/search?uid=1&query=facebook

<element-list><element><term>facebook</term><name>Bing Facebook</name></element></element-list>

I am sending a get request

 $.get('http://localhost:8080/cleo-primer/rest/elements/search', { uid: 1, query: request.term }, function(responseData){alert(responseData);});

but this gives me a blank response and when I check using my firebug, it shows XML Parsing Error: no element found Location: moz-nullprincipal:{1e7688d3-7b3c-43ae-9142-5b183a7cbfda} Line Number 1, Column 1: inside XML tab

You are doing a cross origin call that is not allowed. You are calling from: http://localhost to http://localhost:8080 . The call isn't executed and there will be NO response. Chrome/Safari will show an error like this in the console:

XMLHttpRequest cannot load http://targeturl Origin http://localhost is not allowed by Access-Control-Allow-Origin.

More info: http://en.wikipedia.org/wiki/Same_origin_policy#Origin_determination_rules

If you can make modifications to the back-end, making it send the right headers, will fix your problem... ( Access-Control-Allow-Origin yourdomain or Access-Control-Allow-Origin * for allow all)

More info: http://enable-cors.org

A couple of other options to be complete...

  • If you use apache: You can use an apache proxy to forward (map) location on port 8080 to a different port (like the default port 80) I've posted a question/answer about it a while ago...
  • You could also resort to jsonP (it's not xml but json) using a technique that is loading your resources through a scripttag, you can load json if you're json is adapted to it. (wrapped it a callback function call)
  • If you can't do that either your only option is a proxy script . A proxy script is a sort of middleware. You make a request to the script the script gets the data, and returns it to you. For example php proxy . You can make the same thing in asp, jsp, flash or even java applet.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM