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.
Related
I am trying to fetch data from text file which resides on server. I have access of that location and able to see content when I put URL in browser tab.
I am trying to make AJAX call and get file content, but I am getting Error: Uncaught SyntaxError: Unexpected identifier
Code
function logResults(json){
console.log(json);
}
$.ajax({
url: u,
dataType: "jsonp",
jsonpCallback: "logResults"
});
on console,
I tried below code too, but same result,
$.ajax({
type: 'GET',
url: u,
crossDomain: true,
dataType: 'jsonp',
async: false,
headers: {
"Access-Control-Allow-Origin": "*"
},
success: function(succ) {
console.log("Success: ", succ)
},
error: function(err) {
console.log("Error: ", err)
}
});
This code is always going into error block.
You said:
dataType: "jsonp",
But the URL is responding with:
Deployment automatically finished…
Which is not JSONP, it is plain text.
You get the error when it tries to execute the plain text as JSONP.
Don't put wrong information in the dataType field.
async: false,
That's deprecated. Don't use it. It's also pointless since you are using callbacks.
It's also incompatible with JSONP so it is ignored (until you remove the incorrect dataType).
crossDomain: true,
This has no effect unless you are making a:
non-JSONP request
to the same origin
which gets redirected to a different origin
… which is very rare.
headers: {
"Access-Control-Allow-Origin": "*"
},
Access-Control-Allow-Origin is a response header. It doesn't belong the request. Trying to add it to the request will cause extra problems as it will make the request preflighted.
(At least, that would be the case if you hadn't said dataType: 'jsonp' because adding request headers is incompatible with JSONP).
All you need on the client
The only code you need on the client is:
function logResults(result){
console.log(result);
}
$.ajax({
url: u,
success: logResults
});
The server you are requesting the data from will need to use CORS to grant you permission to access it if it is a cross-origin request.
It is because you have added dataType as jsonp, so that it will try to parse the response to JSON and if the response is not a JSON it will throw error.
As part of my requirement, I would like to generate authorization code by calling O365 url from jquery ajax call. I am calling below script from document ready()
$.ajax({
type: 'GET',
dataType: 'jsonp',
async: false,
url: 'https://login.microsoftonline.com/{{tenant id}}/oauth2/authorize?client_id={{client id}}&response_type=code&redirect_uri=https://myurl.com/createpage/&response_mode=query&resource={{resource}}&state=9128',
crossDomain: true,
success: function(jsonData) {
alert(jsonData);
},
error: function(request, textStatus, errorThrown) {
console.log(errorThrown);
},
cache: false
});
But it is getting following error.
Jquery ajax error
Request you to help on resolving the issue.
You should check the content of the response in the tab network (in your dev tools).
I guess the response is not a valid json, so the parsing failed (your specified the jsonp type in your ajax call)
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.
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.
Running on Chrome I get this error message:
Uncaught SyntaxError: Unexpected token
This is the part of my code which is responsible for the request:
function wetter() {
$.ajax({
'Accept': 'application/json',
type: 'GET',
url: '[here comes the url',
dataType: 'jsonp',
success: function (data) {
//content
}
});
};
You are trying to make a jsonp request to a script that sends json, similar to this. You cant just drop a p after the json and expect it to work.
The datatype it's receiving is probably not what it expects. You may be returning a JSON object while the expected result is JSON with padding (JSONP). You can either try returning JSONP type data or change the datatype in your code above to JSON.