JSONP function with integral callback - javascript

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.((.));/)')"

Related

how to implement asynchronous get in ajax

what should I write in the script to make javascript asynchronously implement firstly ajax method GET and then another simple function(in the order of my script)?
because here while debugging I see that getCategories() is implemented before WriteCategories(categories)
$.ajax({
url: '/api/ListingAPI/GetCategoriesById?id=1',
type: 'GET',
dataType: 'json',
success: function WriteCategories(categories) {
// ...
}
});
var test = getCategories();
Actually you want synchronous execution,
you need to add async: false in ajax request, (But be aware of this it will freeze your UI until your request completes, actually it is not recommended)
$.ajax({
url: '/api/ListingAPI/GetCategoriesById?id=1',
type: 'GET',
async: false,
dataType: 'json',
success: function WriteCategories(categories) {
// ...
}
});
var test = getCategories();
or either you can call your function on ajax success callback,
$.ajax({
url: '/api/ListingAPI/GetCategoriesById?id=1',
type: 'GET',
dataType: 'json',
success: function WriteCategories(categories) {
var test = getCategories();
}
});
For your knowledge, You can pass your query string params in data option in ajax rather than query string itself,
var queryStringData = {id : 1}
$.ajax({
url: '/api/ListingAPI/GetCategoriesById',
type: 'GET',
dataType: 'json',
data: queryStringData,
success: function WriteCategories(categories) {
var test = getCategories();
}
});
Try this code.
$.ajax({
url: '/api/ListingAPI/GetCategoriesById?id=1',
type: 'GET',
dataType: 'json',
success: function WriteCategories(categories) {
var test = getCategories();
}
});
You can call this function after ajax success. By this way AJAX get will be executed first.

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

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);
}
});
}

Why method is not running in JavaScript?

I just runnung method getNewMessagesLong() in longPolling way:
function getNewMessagesLong() {
pollingFishingStarts();
$request = $.ajax({
type: 'POST',
url: "listenMessageLong",
data: lastIncomingMessageLongJson,
dataType: 'json',
success: function(data) {
console.log('Long Fishing ends...');
pollingFishingEnds();
}, complete: getNewMessagesLong})
}
but line with pollingFishingEnds() is not running//
and line with console.log() is runnig ok
The url you have is listenMessageLong. This doesn't seem correct. If the url is not correct, then the ajax will never succeed and thus the success method will never fire.

jQuery Wait until async ajax calls are finished

Hi I have 2 ajax calls in my script, I need them run asnyc to spare time, but I need the second to wait until the first is finished.
$.ajax({
type: "POST",
url: "getText.asmx/ws_getText",
data: parO1,
contentType: "application/json; charset=utf-8",
dataType: "json",
success: function (msg) {
alert(msg.d.data);
},
error: function () {
chyba("chyba v požadavku", "df");
}
});
if (parO2.length > 0) {
$.ajax({
type: "POST",
url: "getText.asmx/ws_getText",
data: parO2,
contentType: "application/json; charset=utf-8",
dataType: "json",
success: function (msg) {
/*WAIT UNTIL THE FIRST CALL IS FINISHED AND THAN DO SOMETHING*/
},
error: function () {
chyba("chyba v požadavku", "df");
}
});
}
So any ideas? Thanks
If using jQuery 1.5+, you can use jQuery.when() to accomplish this. Something like (shortened the ajax calls for brevity, just pass the objects as you're doing above)
$.when($.ajax("getText.asmx/ws_getText"),
$.ajax("getText.asmx/ws_getText")).done(function(a1, a2){
// a1 and a2 are arguments resolved for the
// first and second ajax requests, respectively
var jqXHR = a1[2]; // arguments are [ "success", statusText, jqXHR ]
});
You don't know in which order they will return so if you were rolling this by hand, you would need to check the state of the other request and wait until it has returned.
You need to wire up the second call to be contained within the callback of your first ajax call. Like so:
success: function(msg)
{
alert(msg.d.data);
if(par02.length > 0)
{
// Your 2nd ajax call
}
},
Since JavaScript doesnt run in multiple threads on the client, you can't block the thread until certain conditions are met.
Using jquery
$.ajax({
type: "POST",
async:false, // ** CHANGED **
url: "getText.asmx/ws_getText",
data: parO1,
contentType: "application/json; charset=utf-8",
dataType: "json",
success: function (msg) {
alert(msg.d.data);
}
, error: function () {
chyba("chyba v požadavku", "df");
}
});
Here is another answer on running dual ajax requests. Like Tejs, the user makes an ajax call within the success method...
The poster states You're better off having the success method launch a new ajax request.."
Having two $.ajax() calls in one script

Return value from function with ajax call [duplicate]

This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
Synchronous calls with jquery
I am trying to return a value from a function that contains an ajax call (please see code below).
The returnValue variable is undefined at the alert and when it returns. Can anyone help please?
function getLightboxImages(){
var returnValue;
jQuery.ajax({
url: "http://test.domain.com/WebService.svc/GetAllI",
data: { id: "2", hash:"MaD01" },
async: false,
dataType: "jsonp",
success: function (result) {
returnValue = result
}
});
alert(returnValue);
return returnValue;
}
The alert would have to be placed in the success function, as that is called when ajax returns. What you have above will send the request and then try to alert the returned value before the request is completed.
try this code, for JSONP you have to use callback function rather than success method.
$.ajax({
url: 'http://test.domain.com/WebService.svc/GetAllI',
type: 'GET',
data: "{}",
contentType: "application/json; charset=utf-8",
dataType: "jsonp",
jsonp: "callback",
jsonpCallback: "jsonpCallbackfunction",
error: function () {
alert("Error in Jsonp");
}
});
function jsonpCallbackfunction(responseData) {
alert(responseData);
}
http://cmsnsoftware.blogspot.com/2012/02/how-to-use-cross-domain-ajax-request.html#0
function getLightboxImages(){
var returnValue;
jQuery.ajax({
url: "http://test.domain.com/WebService.svc/GetAllI",
data: { id: "2", hash:"MaD01" },
async: false,
dataType: "jsonp",
success: function (result) {
returnValue = result
}
});
alert(JSON.stringify(returnValue));
return returnValue;
}
Cross-domain requests can only be async, as cross-domain requests rely on a dynamic script tag, which can never be synchronous and must use datatype json and the GET method.
For same domain requests, try stringifying json objects before alerting, like above!
You can't make synchronous JSONP requests.
Use a callback instead of return:
function getLightboxImages(callback) {
jQuery.ajax({
url: "http://test.domain.com/WebService.svc/GetAllI",
data: { id: "2", hash:"MaD01" },
dataType: "jsonp",
success: function (result) {
callback(result);
}
});
}
Usage:
getLightboxImages(function(result){
alert(result);
});

Categories

Resources