I tired to get data off a api using jquery.ajax() and it works fine
here is the code for that:
$.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 );
}
});
Now i wanted to do the same with angularjs so i did the following
First i did this:
var req = {
method: 'GET',
url: "http://api.somesite.com/api?format=json",
headers: {
'Content-Type': "application/json",
},
}
$http(req).then(function(response){
console.log(response.data);
});
i get this in the console:
XMLHttpRequest cannot load http://api.somesite.com/api?format=json. No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http://localhost:8100' is therefore not allowed access.
Then i tried this:
$http.jsonp('http://api.somesite.com/api?format=json', {
headers: {
"content-type":"application/json",
"Accept" : "application/json"
}}).success(function(data, status) {
console.log(status);
console.log(data);
}).
error(function(data, status) {
console.log(status);
console.log(data);
});
I get error code 404 and data as undefined
Kindly do tell how to accomplish. I do not have access to the serve or the api so i cannot modify the api in any way.