0

We have our own Rest Webservices which returns JSON and we are able to consume(GET) data from browser as well as browser based rest client.

We have created a rest client which is JQuery (embeded in html) based and it is not able to even hit the webservices with same information using browser does. Below is code snippet. Only alert under 'error' getting called with status 0 and statusText blank.

$.ajax({
                url: 'http://localhost:8080/appcontext/user/1234',
                type: 'GET',
                dataType: 'json',
                success: function(data) { 
                        alert('GET completed'); 
                },
                error: function(data) { 
                    alert('GET failed STATUS ' + data.status  ); 
                    alert('GET failed TEXT   ' + data.statusText); 
                    }
                }); 

If I change dataType to jsonp then at least call goes to webservices but again goes to 'error' with status 200 and statuText load.

Please help where is the problem. Looks like no problem on webservices side as we can consume using other means like java code, browser etc. Problem with JQuery call.

halfer
  • 19,824
  • 17
  • 99
  • 186
Dynamic Meta
  • 71
  • 13
  • You are going to have to debug in browser and see what is really being returned by the xhr. Guess off the top of my head is an error in translating the return to json, but that is only a guess since you're not providing enough info. – Chris Caviness Sep 10 '14 at 02:52
  • Chris, Thanks for response. I am debugging in firefox and didnt find any information which can help in xhr. Because no data I found in return. readyState = 4, status=200 and statusText=load if i pass type:'jsonp' – Dynamic Meta Sep 10 '14 at 03:14
  • I think response content-type error. Response is not valid json or not content-type header is defined in response header. – KyawLay Sep 10 '14 at 03:20
  • Try doing the call via postman or API rest client. from browser. – SSS Sep 10 '14 at 03:20
  • As i mentioned webservices is returning valid json from browser based client. Apart from browser based client (postman) I tried with even calling from java and php based rest client it worked. Problem looks like very simple but wasted more than a day. – Dynamic Meta Sep 10 '14 at 03:25

1 Answers1

0

this happens because you might be calling the web service from a differnet platform....that is a cross origin hence we need to enable the function

add the following in the web.xml

<filter>
    <filter-name>cross-origin</filter-name>
    <filter-class>org.eclipse.jetty.servlets.CrossOriginFilter</filter-class>
</filter>
<filter-mapping>
    <filter-name>cross-origin</filter-name>
    <url-pattern>/*</url-pattern>
</filter-mapping>

and add the respective jar file: jetty-all

kautilya hari
  • 213
  • 1
  • 3
  • 9