You are using your HTML code from a different host than the host you are requesting. Because of this, you are blocked by the same origin policy .
One way to use JSONP . This allows cross site requests.
In JSON you will be returned:
{a: 5, b: 6}
In JSONP, JSON is wrapped in a function call, so it becomes a script, not an object.
callback({a: 5, b: 6})
You need to edit the REST service to accept a parameter called callback , and then use the value of this parameter as the name of the function. You should also change the content-type to application/javascript .
For example: http://localhost:8080/restws/json/product/get?callback=process should output:
process({a: 5, b: 6})
In your JavaScript, you will need to tell jQuery to use JSONP. For this you need to add ?callback=? to the url.
$.getJSON("http://localhost:8080/restws/json/product/get?callback=?", function(data) { alert(data); });
If you use $.ajax , it will automatically add ?callback=? if you tell him to use jsonp .
$.ajax({ type: "GET", dataType: "jsonp", url: "http://localhost:8080/restws/json/product/get", success: function(data){ alert(data); } });
Rocket Hazmat Aug 18 '11 at 19:15 2011-08-18 19:15
source share