jsonp returns the json but doesnt go into the success block.How do I capture the data then?

StackOverflow https://stackoverflow.com/questions/23658920

  •  22-07-2023
  •  | 
  •  

سؤال

Hi I am new to angular and trying to learn things.

I was trying to get a json file from cross domain. This is the piece of code that I have written. Now I have found out that in spite of the fact that JSON gets returned from the server and I get a 201 Status response. The error block gets hit.

From one of the links in stack overflow I found that one cannot use error handling with async ajax calls. What would be a good way to bind the data to $scope.greeting then?

$http.jsonp('http://marco.factset.com/caches.json').
    success(function(data) {
        $scope.greeting = data;
        alert($scope.greeting);
    }).
  error(function(data, status) {
      console.log( data + " Request failed " + status);
      $scope.status = status;
  });

});

Following are the http reponses as seen in firebug.

Response Headersview source
Cache-Control   no-cache
Connection  close
Content-Encoding    gzip
Content-Length  12178
Content-Type    application/json; charset=utf-8
Date    Wed, 14 May 2014 16:31:27 GMT
Server  Mongrel 1.1.5
Set-Cookie  _marco4_session=BAh7CDoPc2Vzc2lvbl9pZCIlODBiOTFjMDA2MDcyM2NhYzE1MjA2NDE5NDQ0ZjdhOTgiDmFzOmNhY2hlc3sGOglsaXN0ewAiCmZsYXNoSUM6J0FjdGlvbkNvbnRyb2xsZXI6OkZsYXNoOjpGbGFzaEhhc2h7AAY6CkB1c2VkewA%3D--d4c4f3af3a2fe24cde206eaea2bd8097601c48c5; path=/; HttpOnly
Status  201
Vary    Accept-Encoding
Via 1.1 marco
X-Runtime   464
Request Headersview source
Accept  */*
Accept-Encoding gzip, deflate
Accept-Language en-US,en;q=0.5
Authorization   Basic ZHJveTphcHJpbEAxMjM=
Cache-Control   max-age=0
Connection  keep-alive
Cookie  _marco4_session=BAh7CCIOYXM6Y2FjaGVzewY6CWxpc3R7ADoPc2Vzc2lvbl9pZCIlODBiOTFjMDA2MDcyM2NhYzE1MjA2NDE5NDQ0ZjdhOTgiCmZsYXNoSUM6J0FjdGlvbkNvbnRyb2xsZXI6OkZsYXNoOjpGbGFzaEhhc2h7AAY6CkB1c2VkewA%3D--b1fbe53b408aad4d99240aa9b9348984dd7afca4
Host    marco.factset.com
Referer http://localhost:63342/qaweb3/app/index.html
User-Agent  Mozilla/5.0 (Windows NT 6.1; WOW64; rv:29.0) Gecko/20100101 Firefox/29.0 FirePHP/0.7.4
x-insight   activate

Following is an excerpt from the JSON that I get as a reply.

[{"cache": {"config": "/home/fds/metp/FDSadx_tp/etc/adx_tp.cfg", "updated_at": "2012-11-15T02:00:20Z", "product_developer_id": 55, "name": "adx", "secondary_engineer_id": 42, "ftok": 43, "cachetype": "tp", "primary_engineer_id": 66, "service_name": null, "id": "adx", "created_at": "2012-02-22T09:01:41Z"}}, {"cache": {"config": "/home/fds/metp/FDSadx_xstream_tp/etc/adx_tp.cfg", "updated_at": "2014-04-17T10:56:14Z", "product_developer_id": 55, "name": "adx_xstream", "secondary_engineer_id": 24, "ftok": 42, "cachetype": "tp", "primary_engineer_id": 70, "service_name": null, "id": "adx_xstream", "created_at": "2013-11-21T11:45:03Z"}}, {"cache": {"config": "/home/fds/metp/FDSamman_tp/etc/amman_tp.cfg", "updated_at": "2011-12-12T19:47:02Z", "product_developer_id": null, "name": "amman", "secondary_engineer_id": 22

EDIT 1:

The server doesnt support jsonp so using callbacks doesnt work as the json data is not wrapped by function name that is passed as a parameter. Is there any work around to get the cross domain http request working?

 function handle_data(data) {
          // `data` is now the object representation of the JSON data
          alert("Got data " +  data );
      }

       var url = "http://http://marco.factset.com/caches.json?callback=handle_data";

      $http.jsonp(url).
          success(function(data, status, headers, config) {
            alert("Success")
          }).
          error(function(data, status, headers, config) {
              alert("Failure");
          });
هل كانت مفيدة؟

المحلول

So finally I got this working, the server was not JSONP enabled, however I had the privilege of installing my own JSONP service inside the domain, so I made the jsonp request to this custom service, this service in turn makes a simple get request to the actual service in the same domain and the result gets converted to the jsonp format.

Given below is an excerpt from angular controller.

    $http({method: 'JSONP', url: 'http://unixdeva10.myurl.com:9643/jsonprouter?callback=JSON_CALLBACK&key=caches.json'}).
      success(function(data, status, headers, config) {
          process_response(data);
      }).
      error(function(data, status, headers, config) {
          alert("Failed");
      });

نصائح أخرى

You need to figure out why the error block is hit, fix that issue and proceed to use your success method.

A code of 201 is a success (Created)

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top