jquery ajax Uncaught SyntaxError: Unexpected token : while calling an api - javascript

I am trying to get a json response from the comicvine api but am getting the following error. comicvine.gamespot.com/:1 Uncaught SyntaxError: Unexpected token :
I see my json result, formatted, in the response body but am getting the console error above.
export function getSeriesFromComicVine() {
const url = "http://comicvine.gamespot.com/api/characters/?api_key=f18c6362ec6d4c0d7b6d550f36478c1cd6c04a49&filter=gender:male,name:hawkeye&format=json&callback=?";
$.ajax({
url: url,
// data: {test: "test"},
type: 'GET',
crossDomain: true,
jsonpCallback: 'callback',
dataType: 'jsonp',
jsonp: false,
jsonpCallback: "myJsonMethod"
success: function (data) {
console.log(data);
}
});
}

You need to set format=jsonp not json
the jsonp callback parameter name needs to be json_callback according to comicvine.gamespot.com - I found this out by going to url https://comicvine.gamespot.com/api/characters/?api_key=[your api key]&filter=gender:male,name:hawkeye&format=jsonp in the browser, and it told me what was missing - very friendly API - the response had an error value
"'jsonp' format requires a 'json_callback' argument"
and no need for callback=? in the url - seeing as jquery adds the callback parameter and it isn't named callback
function getSeriesFromComicVine() {
const url = "https://comicvine.gamespot.com/api/characters/?api_key=[your api key]&filter=gender:male,name:hawkeye&format=jsonp";
$.ajax({
url: url,
type: 'GET',
dataType: 'jsonp',
jsonp: "json_callback",
success: function (data) {
console.log(data);
}
});
}

Related

Syntax Error after JSONP request

I'm trying to fetch the data of a JSONP, but it returns a Syntax error when the function is called. I'm really new to this topic and don't understand why. Probably because I didn't understand everything of JSONP requests. It would be nice if someone could help me out.
Executed code:
$.ajax({
url: "https://backpack.tf/api/IGetMarketPrices/v1?format=json&callback=JSONP&key=XXX&appid=730",
dataType: "jsonp",
data: {format: "json"},
type: "GET",
jsonp: "response",
jsonpCallback: "response",
success: function( response ) {
console.log( response ); // server response
}
});
(I had to censor my API key.)
Error: Uncaught SyntaxError: Unexpected token :
The response is:
{"response":{"success":1,"current_time":1486000891,"items":{"AK-47 | Aquamarine Revenge (Battle-Scarred)":{"last_updated":1485997229,"quantity":80,"value":1092},...
The first : is underlined by Chrome.
Here is the API documentation.
From trial and error - impressive as I have no API key
$.ajax({
url: "https://backpack.tf/api/IGetMarketPrices/v1?format=jsonp&key=XXX&appid=730",
dataType: "jsonp",
type: "GET",
success: function( response ) {
console.log( response ); // server response
}
});
or
$.ajax({
url: "https://backpack.tf/api/IGetMarketPrices/v1",
dataType: "jsonp",
type: "GET",
data: {
format: 'jsonp',
key: 'XXX',
appid: 730
},
success: function( response ) {
console.log( response ); // server response
}
});
To explain... jQuery automatically adds a query parameter called callback on jsonp requests... as the API states that you need to define the callback name in a parameter called callback, you can let jQuery use the default value of callback
Then, jQuery creates a function with a long random name, and sets the value of the callback parameter to that function - so you don't have to worry about the jsonp or jsonpCallback parameters - defaults are good
I must be missing something basic, because this should work!

JQuery ajax get error :unexpected token :

I have to do an ajax get to retrieve data from OData service, this is my code :
$.ajax({
url : "https://systempath/servicename",
type: "GET", //or POST?
dataType: "jsonp",
xhrFields:
{
withCredentials: true
},
beforeSend: function (request)
{
request.setRequestHeader("Authorization", "Basic");
},
success: function(){alert("ok")},
error: function(){alert("error")}
})
But i get this error :
Uncaught SyntaxError: Unexpected token :
If i consult the network, i find my response in JSON format, but the error function is executed not the success function.
This is my response:
The issue with your code is the dataType that you defined, make sure that the responses headers are corrects with your req. Test with the dataType:'text', this might works fine.

JSONP function with integral callback

I am trying to create a JSONP function that returns the data using a callback from the function that was called, rather than using a seperate callback function.
// JOSONP request.
function jsonp(url,cb){
_jsoncb = function(data) {cb(data)}
$.ajax({
type: 'GET',
url: url,
async: false,
jsonpCallback: '_jsoncb',
contentType: "application/json",
dataType: 'jsonp'
});
}
This works in Firefox, but not in Chrome ?
The error is: "TypeError: undefined is not a function (evaluating 'res.match(/jQuery.((.));/)')"

ajax post method treated as get by restclient

I got an url let’s say abc its type is post. if I try to call it as $.ajax method post it is showing method not allowed 405 error. it I sent by method get it is working fine but the business is not done how to solve the issue?
js code:
$.ajax({
type: "POST",
url: url,
data: data,
beforeSend: function (request){
request.setRequestHeader("X-CSRF-TOKEN", token)
},
success: function(res){
console.log(res)
},
error: function(){
JSON.parse(this.error.arguments[0].responseText).error.message.value
},
dataType: "json"
});
You should use 'method' instead of 'type'. Try this:
$.ajax({
method: "POST",
url: url,
data: data,
beforeSend: function (request){
request.setRequestHeader("X-CSRF-TOKEN", token)
},
success: function(res){
console.log(res)
},
error: function(){
JSON.parse(this.error.arguments[0].responseText).error.message.value
},
dataType: "json"
});
Or you can use the jQuery.post method.
405 Method Not Allowed
The method specified in the Request-Line is not allowed for the resource identified by the Request-URI. The response MUST include an Allow header containing a list of valid methods for the requested resource. 405 Method Not Allowed

Jsonp returned from ajax doesn't show on console

I'm calling a page (external, on other domain), with this code:
var instagram_container = $('div#instagram-answer');
if (instagram_container.length>0)
{
var url = 'http://www.xxxx.it/admin/get_instagram_token';
$.ajax({
type : 'GET',
url : url,
async : false,
jsonpCallback: 'jsonCallback',
contentType: 'application/json',
dataType: 'jsonp',
success: function(response)
{
console.log(response);
instagram_container.html(response.client_id);
},
error: function(e)
{
console.log(e);
}
});
}
I have answer with console.log(e) (basically it recognizes as error)?
Object { readyState=4, status=200, statusText="success", altri elementi...}
But in Firebug, under NET tab, I have the right answer...
This is the console. Line 19 is exactly the
console.log(e);
in error section.
My goal is obtain that Json. Thank you
Is your server returning plain text, or an actual JSON file? Jquery will ignore the response if the server returned a string instead of JSON.
The response type should be: "application/json"
EDIT:
I'd recommend you to use the basic jquery ajax call:
$.ajax({
url: "test.html",
cache: false
})
.done(function( response ) {
console.log(response);
});

Categories

Resources