jQuery Ajax call runs in error but firebug shows 200 ok and returned json

3

I've read a lot of threads about ajax problems, but I've still got no clue, what could cause my problem:

I'm trying to call a RESTful service (internal) via ajax to get a large json-String.

EDIT: This is how I resolved the problem for now - I know that this is not quite a good solution, but since I can't change anything on the webservice for now this is what I use:

<!DOCTYPE html>
<html>
<head>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js">
</script>
<script>
jQuery(document).ready(function(){
    jQuery.support.cors = true;
    jQuery.ajax({
        url: "<internalPage>",
        dataType: 'json'
    })
    .error(function(xhr, err, status) {
        console.log(xhr + err + status);
    })
    .then(function(data) {
        alert("success for me");
        $.each(data.portalServices, function(index, value) {
            $("#someId").append('<li>' + value.name + '</li>');
        });
    });
});

</script>
</head>
<body>
<ul id="someId"></ul>
</body>
</html>

EDIT: This only works in IE not in FF.

This is what the alert says: Ajax Error Type: GET Requesting Page: Status: 0 - error Error Thrown:

But in Firebug:

Response-Header
Content-Type    application/json
Date    Thu, 06 Mar 2014 07:44:32 GMT
Server  Apache-Coyote/1.1
Transfer-Encoding   chunked
Anfrage-HeaderQuelltext anzeigen
Accept  application/json, text/javascript, */*; q=0.01
Accept-Encoding gzip, deflate
Accept-Language de-de,de;q=0.8,en-us;q=0.5,en;q=0.3
Cache-Control   no-cache
Connection  keep-alive
Host    xxxxxx
Origin  null
Pragma  no-cache
User-Agent  Mozilla/5.0 (Windows NT 6.1; WOW64; rv:27.0) Gecko/20100101 Firefox/27.0

Responce-Header from Cache
Content-Type    application/json
Date    Thu, 06 Mar 2014 07:44:32 GMT
Server  Apache-Coyote/1.1
Transfer-Encoding   chunked

Status is 200 OK Response is a valid Json (Really, I've validated it online) {"portalServices":[{"articleID":"ABR_1043024010","name":"SK04 Managed Server: Citrix Basisservice","portalname":"zentrale Arbeitsplatz-Umgebung (Citrix-Basis)","preis":18.0000,"beschreibung":"Mit diesem Produkt stellen wir Ihnen eine zentrale Arbeitsplatz-Umgebung (auf der Basis von Citrix) bereit, die folgende Komponenten ....

But there is no JSON tab.

When I use cache false Firebug does't show me a response and the error shown in the alert is the following:

Ajax Error
Type:GET
Requesting Page: <internalPage>
Status: 0 - Exception ..."Failure" nsresult: "0x80004005 (NS_ERROR_FAILURE)" location "JS frame :: <link to jquery> :: .send :: line 6 " data: no .....

I also tried an ajax call to another service ( "http://rest-service.guides.spring.io/greeting") which succeeded. And there was a JSON tab in firebug, but thats maybe because the json returned from this site is not a list of objects like the json returned from my internal sites webservice.

Any ideas? I think there might be a problem with the webservice, but it would be nice if you got some clues what the cause may be. Thanks a lot.

Even with the following it's still not working =/

headers: {'Access-Control-Allow-Origin': '<internalPage>'},
dataType: 'jsonp',
crossDomain: true

The internalPage is not on the same domain, but would not response to request from an external network. The call to the spring services did work without explicitly allowing crossDomain or setting the header like above.

I will try as suggested here parsererror after jQuery.ajax request with jsonp content type and come back to tell if it has worked.

EDIT: The WebServices will be changed in future so that I can use jsonp instead of forcing jQuery to allow crossDomain Ajax-Calls.

EDIT: The WebService has been changed, so now it works fine with datatype jsonp. =)

Thx for your help.

jquery
ajax
json
web-services
asked on Stack Overflow Mar 6, 2014 by dredg189 • edited May 23, 2017 by Community

2 Answers

0

You sure that this request is causing the error? $(document).ajaxError is a global error handler (called on any error), so may be you have a different ajax call that's causing it

You can change you code to:

 jQuery.ajax({
        url: "http://<internalPage",
        dataType: 'json'
    })
    .error(function(xhr, err, status) {
       console.log(xhr + err + status);
    })
    .then(function(data) {
       alert("success for me");
    });

Try a different browser i.e. Chrome, may be you have some extension that's causing the problem

Are you using the same url i.e. not making cross-domain requests? jQuery Call to WebService returns "No Transport" error

answered on Stack Overflow Mar 6, 2014 by Boklucius • edited May 23, 2017 by Community
0

I think your jquery Ajax code has a problem

try this code

$.ajax({
    url: "http://<internalPage",
    dataType: 'json',
    success:function(data){
       alert(data); 
       // beter to use eval instead of alert
    }

});
answered on Stack Overflow Mar 6, 2014 by Mohammad Kermani

User contributions licensed under CC BY-SA 3.0