How to Get the Ajax Response from Success and Assign It in a Variable Using Jquery

How to get the ajax response from success and assign it in a variable using jQuery?

$.ajax({
type: 'POST',
url: password_url,
data: '',
dataType: 'json',
success: function(response){
var k=response;

if(k.indexOf("1") != -1)
$('#existing_info').html('Password is VALID');
else
$('#existing_info').html('Password is INVALID!');
}
});

response is in response variable of success function.

indexof returns the index within the calling String object of the first occurrence of the specified value, starting the search at fromIndex,
returns -1 if the value is not found.

Return value from ajax and set that value to a variable

Use callbacks

function ajaxCall(callback){
$.ajax({
url: url_of_my_php_file,
dataType: 'json',
type: 'GET',
contentType: 'application/json',
success: callback
});
}

ajaxCall(result => {
//do something with result
})

Or Promises

function ajaxCall(callback){
return new Promise((resolve, reject) => {
$.ajax({
url: url_of_my_php_file,
dataType: 'json',
type: 'GET',
contentType: 'application/json',
success: resolve,
error: reject
});
});
}

ajaxCall()
.then(result => {
//do something with result
})
.catch(err => {
//or err
});

jquery - return value using ajax result on success

The trouble is that you can not return a value from an asynchronous call, like an AJAX request, and expect it to work.

The reason is that the code waiting for the response has already executed by the time the response is received.

The solution to this problem is to run the necessary code inside the success: callback. That way it is accessing the data only when it is available.

function isSession(selector) {
$.ajax({
type: "POST",
url: '/order.html',
data: ({ issession : 1, selector: selector }),
dataType: "html",
success: function(data) {
// Run the code here that needs
// to access the data returned
return data;
},
error: function() {
alert('Error occured');
}
});
}

Another possibility (which is effectively the same thing) is to call a function inside your success: callback that passes the data when it is available.

function isSession(selector) {
$.ajax({
type: "POST",
url: '/order.html',
data: ({ issession : 1, selector: selector }),
dataType: "html",
success: function(data) {
// Call this function on success
someFunction( data );
return data;
},
error: function() {
alert('Error occured');
}
});
}

function someFunction( data ) {
// Do something with your data
}

Ajax success not setting value of variable

You can make a function ,so whenever response comes you can assign value to your variable like below:

var countAnchors=null;

function result(r){
countAnchors= r;//assigning value return
console.log("countAnchors: ",countAnchors);
}

$.ajax({
type: "POST",
url: "readString.php",
data: { 'fn': filename },
success: function(response){
result(response); //calling function
},
error: function (jqXHR) {
console.log(jqXHR);
alert("There was a problem");
}
});

jQuery ajax request: how to access sent data in success function?

Generally, if you want to be able to reference data more than once, you need to make sure it's in the proper scope. The scope of the json data object you're passing inside .ajax() is the ajax function. If you want to be able to reference the value of data: outside of that, for example the scope in which you're calling .ajax(), the simplest way is just to assign it to a variable. eg

myData = { myVar: "hello" };$.ajax({  url: "whatever.php",  method: "POST",  data: myData,  success: function(response) {    console.log('received this response: '+response);    $("#response").html(response);    console.log('the value of myVar was: '+myData.myVar); // <<<< data.myVar is not accessible from here    $("#myVar").html(myData.myVar);  }});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script><p id="response"></p><p id="myVar"></p>

How to return data to variable after ajax call success

$.ajax({
url:"operation.php",
dataType: "text",
success:function(data) {
doSomthingOnComplete(data);
}
});

function doSomthingOnComplete(data)
{
// do here your work
}


Related Topics



Leave a reply



Submit