ionic Angularjs vs jquery : $http.jsonp() vs jquery.ajax

时间:2015-07-08 15:45:20

标签: javascript jquery ajax json angularjs

I am trying to get some json data from an api it works with jquery but doesn't work with angularjs?

I am using Ionic to code the app.

I don't think it is a CORS issue, if it were the jQuery code wouldn't work.

in jQuery i do the following: (Working)

$.ajax({
 url: "http://api.somesite.com/api?format=json",
  type: "GET", //it works with POST as well
  dataType: 'jsonp',
 })
  .done(function( data ) {
    if ( console && console.log ) {
      console.log( "Sample of data:", data );
    }
  });

console result: http://imgmonkey.com/hfrk2cbfz1gi/firstSuccess.png.html

This is the Angularjs Code: (Not Working)

 url="http://api.somesite.com/api?format=json";

 $http.jsonp(url)
 .success(function(data, status, headers, config) {
   console.log(data);
    console.log(status);
     console.log(headers);
    console.log(config);
})
.error(function(data, status, headers, config) {
    console.log(data);
    console.log(status);
     console.log(headers);
    console.log(config);
});

console result: http://imgmonkey.com/v16ug212dm0d/second_fail.png.html

1 个答案:

答案 0 :(得分:1)

if you want to use jsonp with angular you need a query string of "callback=JSON_CALLBACK"

url="http://api.somesite.com/api?format=json&callback=JSON_CALLBACK";