Reputation: 1071
I have REST service for both XML and JSON formats:
http://localhost:5050/rest/rest/report/check/{id}/{checksum}.xml http://localhost:5050/rest/rest/report/check/{id}/{checksum}.json
Example: calling http://localhost:5050/rest/rest/report/check/420/339d9146ddd3d6646a1fe93ddf4d7ab8c4a51c61.xml will return result:
<report>
<id>420</id>
<checksum>339d9146ddd3d6646a1fe93ddf4d7ab8c4a51c61</checksum>
<checksumValid>true</checksumValid>
<reportName>sprawozdanie 1</reportName>
<userName>John Smith</userName>
<state>robocze</state>
</report>
Now I want to call that REST service from JQuery (either xml or json, i don't care).
What I do is:
$.ajax({
type:"GET",
url:"http://127.0.0.1:5050/rest/rest/report/check/" + obj.id + "/" + obj.checksum + ".xml",
success:function (data, textStatus) {
alert('success...');
},
error:function (xhr, ajaxOptions, thrownError) {
alert("thrown: '" + thrownError + "', status: '"
+ xhr.status + "', status text: '"
+ xhr.statusText + "'");
}
});
and I end up with error function being called, with result:
thrown: '', status: '0', status text: 'error'
What am I doing wrong?
Upvotes: 1
Views: 475
Reputation: 2923
Agreed with same origin policy. Here's a quick way to test if this is the case:
If it works, then your issue is same origin policy. This policy is a pain for working in a development environment, because you have to host your web service on the same host (no aliases allowed) AND port as the site hosting the JavaScript.
You can get both a service and your website hosted via the same server using a Proxy. This article is a great reference for Apache (WAMP), if that's what you happen to be using:
Upvotes: 0
Reputation: 1071
Ok, so the problem was trivial and apparently I used to hak it some time ago and forgot about it (damn i need a wiki :) ).
Any way the problem is so called 'cross domain' which was prohibited by deafult in my spring-mvc rest project.
What I did was creating a new filter
public class OptionsHeadersFilter implements Filter {
public void doFilter(ServletRequest req, ServletResponse res, FilterChain chain)
throws IOException, ServletException {
HttpServletResponse response = (HttpServletResponse) res;
response.setHeader("Access-Control-Allow-Origin", "*");
response.setHeader("Access-Control-Allow-Methods", "GET,POST,PUT,DELETE");
response.setHeader("Access-Control-Max-Age", "360");
response.setHeader("Access-Control-Allow-Headers", "x-requested-with");
chain.doFilter(req, res);
}
public void init(FilterConfig filterConfig) {
}
public void destroy() {
}
}
and add it in my web.xml
<filter>
<filter-name>OptionsHeadersFilter</filter-name>
<filter-class>poi.rest.OptionsHeadersFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>OptionsHeadersFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
Upvotes: 0
Reputation: 120178
you need to use localhost
instead of 127.0.0.1
due to same origin policy.
Upvotes: 2