17
12
|
I'm trying to make an ajax call from jquery to a rest service. The rest service used is right from a tutorial of mkyong's blog, this one: http://www.mkyong.com/webservices/jax-rs/integrate-jackson-with-resteasy/
The service works, but when i try to make a call from jQuery, in Firebug there is a 200 status code, but in the response section, nothing.
Here is the html page with the ajax call:
|
I can't figure it out where I went wrong, could you please tell me what i am doing wrong?
Thanks!
35
|
You are running your HTML from a different host than the host you are requesting. Because of this, you are getting blocked by the same origin policy.
One way around this is to use JSONP. This allows cross-site requests.
In JSON, you are returned:
In JSONP, the JSON is wrapped in a function call, so it becomes a script, and not an object.
You need to edit your REST service to accept a parameter called
callback , and then to use the value of that parameter as the function name. You should also change the content-type toapplication/javascript .
For example:
http://localhost:8080/restws/json/product/get?callback=process should output:
In your JavaScript, you will need to tell jQuery to use JSONP. To do this, you need to append
?callback=? to the URL.
If you use
$.ajax , it will auto append the ?callback=? if you tell it to use jsonp .
| ||||||||||||||||||||
|
http://localhost:8080
as well? If it's not (http://localhost/
is not the same), you'll have to make your API output JSONP. – Rocket Hazmat Aug 18 '11 at 19:02