AngularJS issue parsing json using $HTTP.JSONP

301 views Asked by At

I'm trying to fetch data from wooCommerce api in an angularjs application using jsonp method

$http.jsonp('http://pwoo.dev/wc-api/v2?callback=JSON_CALLBACK')
.success(function ( data, status, headers, config ) {
    console.log(data, status, headers, config);
})
.error(function ( data, status, headers, config ) {
    console.log("Error", data, status, headers, config);
});

and in return it gives me this error in console

Uncaught SyntaxError: Unexpected token :

and breaks the code in error block

http://jsfiddle.net/zzlalani/5Lj46p69/

here is the live link from same type of api I used in fiddle above

https://sergedenimes.com/wc-api/v2

I have validated the json online and its correct and valid in format, you check it here in fiddle's console in the following link

https://jsfiddle.net/zzlalani/ru6kozxn/1/

Update:

Ok its not working for jQuery.ajax as well check the fiddle below where I have used simple jquery's $.ajax method with dataType: "jsonp"

https://jsfiddle.net/zzlalani/zvreh2xp/2/

I don't understand where is the issue because in my above angular example a simple test.json with data

{
    "name": "zeeshan Lalani"
}

throws same error. What m'I missing here? How people in this world use JSONP examples ? :(

1

There are 1 answers

0
Walter Chapilliquen - wZVanG On

The jsonp content should be between a callback function like:

JSON_CALLBACK({"data":{"A":123,"B":true}})