JSONP and Framework7 - javascript

I'm trying to get images from Instagram public api via ajax and JSONP:
var target = https://www.instagram.com/p/BP3Wu_EDXsjdT5Llz13jFv2UeS0Vw0OTxrztmo0/?__a=1?callback=?';
$$.ajax({
type: "GET",
dataType: 'json',
crossDomain: true,
url: target,
success: function(data){
console.log(data);
},
error: function(xhr,status){
console.log("Error"+status);
}
});
I'm getting: Uncaught SyntaxError: Unexpected token <.
What's wrong?
Thanks

A few mistakes...
var target = 'https://www.instagram.com/p/BP3Wu_EDXsjdT5Llz13jFv2UeS0Vw0OTxrztmo0/?__a=1&callback=';
Changes: Missing ' at the beginning and changed second ? with &
Should work fine

That API with ?__a=1 is undocumented API and does not support JSONP, so you cannot make client side API call using AJAX, you have to make a server side http request and it will work.

Related

"Uncaught SyntaxError: Unexpected token <" after successful HTTP Request

I am trying to do AJAX using jQuery with an ODATA REST service provided by an SAP backend system.
$.ajax({
type: 'GET',
url: 'http://sapxx.sapms/sap/opu/odata/.../',
async: true,
dataType: 'jsonp',
username: 'username',
password: 'password',
crossDomain: true,
success: function() {
alert('Done.');
},
error: function() {
alert('Error.');
},
});
The request returns status 200 and I get a response from the server which is called something like "?callback=jQuery3100687..." and contains xml code. All this is visible in the Chrome debugger. But after the successful HTTP Request I get the aforementioned error
Uncaught SyntaxError: Unexpected token <
I suspect the error is due to the "dataType" parameter which is set to "jsonp" in the request. Is there any way to work around this error? The server can only respond using XML format. The request only works when the dataType is set to "jsonp", I guess because it enables CORS. After sending the request, I get the "Error" alert despite the 200 status.
You say you get an xml response from the server. Your ajax request is set up for jsonp, though:
dataType: 'jsonp',
Try this instead:
dataType: 'xml',
success: function(xml) {
//remainder of the code
}
If you set the dataType to jsonp, jQuery will try to parse the response as JSON. This results in errors, because the response ins't JSON.

Ajax request failing?

I am trying to retrieve json data from an api.
It keeps failing, but when I look in the Net tab of Firebug I can see that the GET request executed and returned the correct data. Am I doing something wrong or does anyone have tips on how to debug this?
Edit: Have changed to dataType json and error status code is 0
Thanks
$.ajax({
url: 'http://localhost:55894/api/Test/All',
data: {
format: 'json'
},
error: function () {
alert('Error');
},
dataType: 'jsonp',
success: function (data) {
alert('Ok');
},
type: 'GET'
});
From the info you provided the reason it is failing is because you dont have a cross domain access policy setup. Because you are using different ports to host the website and the API you are running into this issue. You can either setup a crossdomain.xml with the proper security settings or you can move both the API and the webserver to the same port.
Have a look at this for more info: http://en.wikipedia.org/wiki/Same-origin_policy
u can try this way:
$.ajax({
type: 'GET',
url: 'url api here',
beforeSend: function() {
},
success: function(data) {
},
error: function(xhr) { // if error occured
},
complete: function() {
},
dataType: 'json'
});
JSON and JSONP are different. If you are using JSONP, the server side must be prepared to support it. Doesn't look like you are using JSONP.
So just change dataType to 'json', as you are "trying to retrieve json data".

Uncaught SyntaxError: Unexpected token : ajax call

So I'm trying to query the below json feed, however I keep getting the error in the topic.
I've searched around this site for possible answers however none that I've come across have worked so far. Commented out datatype and jsonp, jsonpCallback isnt it either, either is data, I've made sure that it validats via http://jsonformatter.curiousconcept.com/ and it does. I really dont know.
$.ajax({
type: 'GET',
url: 'http://raidbots.com/json/playerdata/us/mannoroth/usiris',
cache:true,
dataType: 'jsonp',
data: {
format: 'json',
},
success: ranks,
jsonpCallback:'callbackName',
error: function(data) { console.log(data); },
jsonp: false,
});
function callbackName(data){
console.log("jsonpCallback");
}
var ranks = function(data) {
console.log(data);
}
Thank you
-Art
The error is in your JSONp data because it's just JSON and not JSONp. JSONp requires the document to be valid JavaScript containing a function call.
If they don't support jsonp you need to use a proxy script (e.g. a php script on your server that retrieves the document) or ask them to send CORS headers so you can use a normal non-JSONp AJAX call to retrieve the data directly.

Rails: Get remote page via ajax

I use Rails3 and I try to get remote page via ajax. (https://play.google.com/store/apps/details?id=).
$.ajax({
url: app_url,
type: 'GET',
data: "id=<id>",
crossDomain : true,
dataType: 'jsonp',
success: function ( code ) {
alert("Good.");
}
});
When I run the script, the I see: "Uncaught SyntaxError: Unexpected token < " error message.
By the way, I tried do it as:
$.ajax({
url: app_url,
type: 'GET',
data: "id=<id>",
crossDomain : true,
dataType: 'jsonp',
success: function ( code ) {
alert("Good.");
}
});
But I see "Origin http://example.com:3000 is not allowed by Access-Control-Allow-Origin." error message.
How can I fix the error and get the page ?
Thanks.
If you are trying to access remote pages via AJAX, that page may be blocking your request. The error message would suggest this: https://developer.mozilla.org/en-US/docs/HTTP_access_control
EDIT
For clarity, Access-Control-Allow-Origin is the server setting which "origins" are allowed to retrieve from it. You could possibly grab this page server-side, and depending on google's level of security, you may have to spoof a browser. PHP CURL comes to mind. You would then set up an ajax call to your server script, your server would go get the page for you, then return it to you ajax call.

jQuery an Yahoo Weather API call failed

I want to catch weather information. I'm trying to do it with jQuery. So here is my Code:
$(document).ready(function(){
var weatherURL = 'http://weather.yahooapis.com/forecastjson?w=20066287&u=c&callback=?';
$.getJSON(weatherURL, function(data){
//console.log('done');
}); });
It seems that this is working. But it outputs me
Uncaught SyntaxError: Unexpected token :
I think its an JSON validation problem. But all online JSON validation tool passes the test.
It seems the API returns JSON, not JSONP data; getJSON automatically tries to parse it as JSONP is there's a callback query in the URL:
If the URL includes the string "callback=?" (or similar, as defined by the server-side API), the request is treated as JSONP instead.
http://api.jquery.com/jQuery.getJSON/
Try this:
$(document).ready(function(){
var weatherURL = 'http://weather.yahooapis.com/forecastjson?w=20066287&u=c';
$.ajax({
url: weatherURL,
dataType: 'json',
success: function(data) {
//console.log('done');
}
});
});

Categories

Resources