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.

Community
  • 1
  • 1
dredg189
  • 39
  • 1
  • 7
  • Just paste the url which is in your AJAX call into the browser. What do you see? – Sandeep Nayak Mar 06 '14 at 08:27
  • @ Sandeep : I get an Status 500, but I guess thats because the server expects as datatype json and nothing else. When I try calling the service with restclient, I also have to set the type to json to get back a response. – dredg189 Mar 06 '14 at 08:33
  • Status 500 - Internal Server Error. I think the Server isn't sending back a JSON as required. `dataType:'json'` tells the server that the client is expecting a JSON as the response to the REST call. – Sandeep Nayak Mar 06 '14 at 08:38
  • Well it is, Firebug does show the json string on tab response and die firefox plugin restclient is also recieving the json – dredg189 Mar 06 '14 at 08:44

2 Answers2

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

Community
  • 1
  • 1
Boklucius
  • 1,896
  • 17
  • 19
  • my bad, the error function has 3 parameters, but in this case the url is right. – Boklucius Mar 06 '14 at 08:40
  • I dind't see the hint for trying another browser, I tried it in IE 9, the console said: PROTOKOLL: [object Object]errorNo Transport – dredg189 Mar 06 '14 at 08:50
  • 1
    Can you try changing the dataType to `jsonp` – Boklucius Mar 06 '14 at 08:51
  • Thats what I get with this in IE: PROTOKOLL: [object Object]parsererrorError: jQuery1102010325704185623335_1394096145204 was not called – dredg189 Mar 06 '14 at 08:56
  • add `crossdomain: true` in the ajax call – Boklucius Mar 06 '14 at 08:59
  • With jsonp and crossdomain: true I get: PROTOKOLL: [object Object]parsererrorError: jQuery110205253123243668724_1394096694979 was not called. And with json instead I get: PROTOKOLL: [object Object]errorNo Transport – dredg189 Mar 06 '14 at 09:05
  • please take a look at this question: http://stackoverflow.com/questions/5359224/parsererror-after-jquery-ajax-request-with-jsonp-content-type – Boklucius Mar 06 '14 at 09:32
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
    }

});
Mohammad Kermani
  • 5,188
  • 7
  • 37
  • 61