Reputation: 1177
I've made a RESTful web service at localhost in Java. When I type the following url in my navigator address bar :
http://localhost:8080/GeonotesApp2-war/webresources/route/1
It will give me :
{"rid":"1","routeComment":"hhhahahahah","routeDateCreate":"2012-04-03T00:00:00+02:00","routeDistance":"13400.0","routeName":"route1","routeUserId":"1"}
Now I want to use an Ajax call to get this JSON information with this script :
<html >
<head>
<meta charset="utf-8">
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
<script>
$(function(){
$.ajax({
type: "GET",
url: "http://localhost:8080/GeonotesApp2-war/webresources/route/1",
dataType: 'json',
headers: {
Accept: "application/json",
"Access-Control-Allow-Origin": "*"
},
success: function(resp) {
alert("success");
},
error: function(e) {
alert("error: "+e);
}
});
});
</script>
</head>
<body>
Hello Test Service!
</body>
However I'm always getting the error message :
error: [object Object]
I found this post and followed exactly what it does, but doesn't work. Can anyone tell me why? Thanks
Calling Rest webservice using JQuery Ajax call , web service is returning JSON string
Upvotes: 1
Views: 14997
Reputation: 49939
The headers have to be on the service side. So the browser knows that the server allows this website to use Ajax to load the data. So these headers:
Access-Control-Allow-Origin: *
Access-Control-Allow-Methods: GET
Links:
Upvotes: 4