The following jQuery code attempts to store a client's IP address in a global variable during the callback for an ajax GET request.
Code
var userip = null;
$.ajax({
url: "http://ipinfo.io",
type: 'get',
dataType: 'jsonp',
async: false,
success: function(data) {
userip = data.ip;
console.log(userip); // prints unique ip address
}
}).done(console.log(userip)); // prints null
Knowing that GET requests are usually executed asynchronously (but requiring otherwise), I have marked async: false in the JSON spec. For added measure, I call done() to ensure the request has finished before continuing.
Nevertheless, printing userip returns null after the GET request but prints the unique client IP during the ajax success callback.
Any explanation would be greatly appreciated. Thanks in advance.
.done() expects a callback. Try swapping out
console.log(userip)
with
function() { console.log(userip); }
what you're doing is making the console.log(userip) execute immediately, and not after the request is completed. Which is why userip gets stored properly in the success handler. Think of done as what to do once the request is completed. It needs to execute an action (function). You're passing a statement (console.log(userip)) which actually returns undefined. So done doesn't do anything.
try this instead
}).done(function() {
console.log(userip);
});
Try this
var userip = null;
$.ajax({
url: "http://ipinfo.io",
type: 'get',
dataType: 'jsonp',
async: false,
success: function (data) {
userip = data.ip;
console.log(userip); // prints unique ip address
},
complete: function () {
console.log(userip); // prints unique ip address
}
});
Related
I've got a small javascript function that's only purpose is to call a script to get some data from the database so it can be used by other functions on the client side.
I'm using a jQuery call to get the data but for me to pass the object out of the success functions scope I need to turn asynchronous off which raises a deprecation warning.
My function works as intended currently but I'd like to use a method that isn't deprecated. Here is my function:
function getData(ID) {
var Data = {};
$.ajax({
url: 'script',
method: 'POST',
dataType: 'json',
async: false,
data: {action: 'get', id: ID },
success: function(response) {
Data = response;
})
});
return Data;
}
I've changed the variable names for privacy reasons so apologies if they're vague.
Also why is synchronous calls considered harmful to the end users experience?
As AJAX call is asynchronous, you will always get blank object ({}) in response.
There are 2 approach.
You can do async:false
To get response returned in AJAX call try like below code. Which wait for response from server.
function getData(ID) {
return $.ajax({
url: 'script',
method: 'POST',
dataType: 'json',
//async: true, //default async call
data: {action: 'get', id: ID },
success: function(response) {
//Data = response;
})
});
}
$.when(getData(YOUR_ID)).done(function(response){
//access response data here
});
I make a call with Ajax to an IP with some arguments. Before the call that matters (lets call that "call 2") can be executed, it has to make an Ajax call to get a sessionId (and lets call that "call 1"). The sessionId is necessary to make any call to the IP and have it do something with the arguments.
But when I have the function below each other, it first does "call 2" and after that "call 1". Classic async behavior but since in the Ajax call I say async: false, I don't really understand why it isn't doing what I say it has to do.
Complete Ajax Call "call 1":
Url = "_ip_?action=getsessionid";
$.ajax({
url: Url,
type: "Post",
dataType: "jsonp",
async: false,
success: function( json ){
var j = $.parseJSON(json);
var sessionid = j['sessionId'];
},
error: function( errorThrown ) {
console.log(errorThrown);
}
});
And Ajax call "call 2":
Url = "_ip_?action=action&sessionId=" + sessionid;
$.ajax({
url: Url,
type: "Post",
dataType: "jsonp",
async: false,
success: function( json ){
var j = JSON.parse(json);
//do something with j
},
error: function( errorThrown ) {
console.log(errorThrown);
}
});
Just assume variable sessionid does give the sessionid to "call 2".
As you can see, I get an json string returned.
What I did now is that the next Ajax call only gets initialized and executed when the first Ajax call was successfully executed. This seems a workaround to me and not a proper way to do what I try to accomplish.
from JQuery.ajax documentaton:
Cross-domain requests and dataType: "jsonp" requests do not support synchronous operation. Note that synchronous requests may temporarily lock the browser, disabling any actions while the request is active. As of jQuery 1.8, the use of async: false with jqXHR ($.Deferred) is deprecated; you must use the success/error/complete callback options instead of the corresponding methods of the jqXHR object such as jqXHR.done() or the deprecated jqXHR.success().
With that. My solution is to make the next ajax request inside the success function. The JQuery documentation explains this more. http://api.jquery.com/jquery.ajax/
Define var sessionid as global variable and assign the value to the same in ajax call1 and then access this value in call2.
var sessionid='';
Url = "_ip_?action=getsessionid";
$.ajax({
url: Url,
type: "Post",
dataType: "jsonp",
async: false,
success: function( json ){
var j = $.parseJSON(json);
sessionid = j['sessionId'];
},
error: function( errorThrown ) {
console.log(errorThrown);
}
});
I'm trying to run a function before async ajax request. However function is running after async request gets the respond.
Is there any way to solve this issue?
block();
ajaxRequest= $.ajax({
url: siteURL+'includes/ajax/action.php',
type: "POST",
async: false,
data: {productID : productID},
dataType: "json"
});
ajaxRequest.done(function(data) {
block(true);
if (data === false) {
alerts('error title','error info here', 'error', 200);
return false;
}
});
ajaxRequest.fail(function(jqXHR, textStatus) {block(true); alerts('error title','error info','error');});
confirm();
I run more functions after these codes. However as I stated before, block(); function is waiting till async ajax request is getting response.
If I don't run asynchronous, then I get block() and confirm() functions running at the same time so return false; losing all the meaning.
P.S. I run these codes when a form is submitted so if async request is failed I don't want it to run any other code after it. However when it is asynchronously running block() is waiting till response is returned.
Your problem is async:false. Your request is NOT an async request, it is a sync request. It blocks javascript code and browser rendering from happening while the request is being processed. With your current setup, there should be no harm in removing async: false, thus making it async.
block();
ajaxRequest= $.ajax({
url: siteURL+'includes/ajax/action.php',
type: "POST",
data: {productID : productID},
dataType: "json"
});
ajaxRequest.done(function(data) {
block(true);
if (data === false) {
alerts('error title','error info here', 'error', 200);
return false;
}
});
ajaxRequest.fail(function(jqXHR, textStatus) {block(true); alerts('error title','error info','error');});
ajaxRequest.always(confirm);
I'm trying to return a callback from an AJAX submitted form. The user submits a form, the server processes and returns the valid response, i.e. an error message and also a JavaScript function that could perform an action. I'm using Zepto.js faling back to jQuery depending on browser.
My ajax request is:
$.ajax({
success: function(data, status, xhr) {
data.callback();
},
url: form.attr('action'),
data: form.serialize(),
dataType: 'json'
});
On the server I want to return something like:
// PHP code
?>
{
return: false,
error: 'Sorry, we couldn’t find an account with that username or password.',
callback: function() {
console.log('this is the callback');
}
}
<?php
// more PHP code
When returned to the browser callback function should fire. I want the server to return the callback so I can use the same JavaScript code and have it respond accordingly to the server response.
Would I need to change the dataType to script? However I thought this was just for loading .js files, not blocks of code.
Any help appreciated.
The general feeling here is I am approaching this in the wrong way. So revised code:
$.ajax({
success: function(data, status, xhr) {
var callback = data['callback'];
callback();
},
url: form.attr('action'), // in this example it's badLogin
data: form.serialize(),
dataType: 'json'
});
// callback specified in PHP
badLogin: function() {
console.log('bad login');
}
And my PHP
if (!$valid) {
?>
{
"return": false,
"error": "Sorry, we couldn’t find an account with that username or password.",
"callback": "badLogin"
}
<?php
}
Thanks for pointing me in the right direction.
You can always return the code as a string and use eval() if you are absolutely sure that the string will always be correct and no code can be injected.
I am trying to implement Repository pattern in JavaScript. I have ViewModel which i want to initialize with the data when i call Initialize method on it. Everything seems to be falling in places except that i am not able to return the data from my AJAX call. I can see that data is coming back from the ajax call but when i trying to capture the data in SomeViewModel's done function, it is null.
Can someone please point me out where i am going wrong here?
P.S: Please notice that i am not making Async call so the call chain is properly maintained.
This is how my Repository looks like:
function SomeRepository(){
this.LoadSomeData = function loadData()
{
$.ajax({
type: "POST",
url: "someUrl",
cache: true,
async: false,
contentType: "application/json; charset=utf-8",
data: "{}",
dataType: "json",
//success: handleHtml,
success: function(data) {
alert('data received');
return data;
},
error: ajaxFailed
});
function ajaxFailed(xmlRequest) {
alert(xmlRequest.status + ' \n\r ' +
xmlRequest.statusText + '\n\r' +
xmlRequest.responseText);
}
}
};
This is how my ViewModel looks like:
function SomeViewModel(repository){
var self = this;
var def = $.Deferred();
this.initialize = function () {
var def = $.Deferred();
$.when(repository.LoadSomeData())
.done(function (data) {
def.resolve();
});
return def;
};
}
This is how i am calling from an aspx page:
var viewModel = new SomeViewModel(new SomeRepository());
viewModel.initialize().done(alert('viewmodel initialized'));
alert(viewModel.someProperty);
I have used successfully an auxiliar variable to put the ajax result, when ajax call is inside a function (only works if ajax is async=false) and i need the function does return the ajax result. I don't know if this is the best solution.
function ajaxFunction(){
var result='';
$.ajax({
type: "POST",
url: "someUrl",
cache: true,
async: false,
contentType: "application/json; charset=utf-8",
data: "{}",
dataType: "json",
//success: handleHtml,
success: function(data) {
alert('data received');
result=data;
},
error: ajaxFailed
});
return result;
}
Doesn't matter that it's synchronous (though it really shouldn't be). Returning a value from inside the ajax callback will not cause the value to be returned from the containing function.
Using asynchronous ajax is generally a much better idea anyway, but that will force you to create an API that allows its clients to pass in handlers to be called when the ajax request completes. To do that, you'd give your "LoadSomeData" function a parameter. A caller would pass in a function, and your ajax "success" handler would pass on the results (or some transformation of the results; depends on what it is that you're doing) to that callback. It's the same idea as the callbacks used in the ajax call itself.