Click here to Skip to main content
15,914,419 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
i declare one global variable. their is one function in which contain the ajax call and after success the responce stored into tmp value but out side of scope the value of tmp is not persist into alert why i dont no any other solution on it.

What I have tried:

Var tmp=null;

function Fn()
{
var PlantName = function () {
                        var dataPlantName = $.Deferred();
                        $.ajax({
                            type: "POST",
                            contentType: "application/json; charset=utf-8",
                            url: "WebService1.asmx/GetPlantMapName",
                            dataType: "json"
                        }).done(function (response) {
                            //console.log(response.d);
                            dataPlantName.resolve(response.d.data);
                            tmp = response.d.data;                            
                        });
                        return dataPlantName.promise();
                    } ();

alert(tmp);
};
Posted
Updated 2-Dec-19 23:56pm
Comments
F-ES Sitecore 14-Apr-17 10:15am    
It is persisting, the problem is that your alert is running before the tmp variable is set. ajax calls happen in the background while your code continues to run, so your code doesn't stop at $.ajax waiting for the done event, instead it continues on so your alert is empty.

If you want something to happen when the tmp variable is set then write that code in the done event.
Member 12893295 16-Apr-17 10:52am    
Thanks it working for me.

1 solution

add
JavaScript
async:false

refer jQuery.ajax() | jQuery API Documentation[^]
 
Share this answer
 
Comments
Member 12893295 13-Apr-17 4:22am    
Got and error like Synchronous XMLHttpRequest on the main thread is deprecated because of its detrimental effects to the end user's experience
Karthik_Mahalingam 13-Apr-17 4:33am    
its a warning, not an error
which version of jquery are you using?
Member 12893295 14-Apr-17 1:31am    
jquery-1.12.4.js actually i have ajax call inside ajax call so the async =false settitng will impact on other ajax call????
Karthik_Mahalingam 14-Apr-17 1:34am    
try like this
$.ajax({
    type: "POST",
    url: "WebService1.asmx/GetPlantMapName",
    contentType: "application/json; charset=utf-8", 
    dataType: "json",
    async: true,
    success: function (result) {
    }
});

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900