jquery ajax call success, how do i change a global variable in the wrapper javascript function?

前端 未结 2 1473
日久生厌
日久生厌 2020-12-05 23:09
function ajax_test(str1){ 
  var url = \"None\" 
  jq.ajax({
    type:\'post\', 
    cache: false, 
    url: \'http://....\' + str1, 
    success: function(data, sta         


        
2条回答
  •  温柔的废话
    2020-12-05 23:34

    In Javascript, it is impossible for a function to return an asynchronous result. The function will usually return before the AJAX request is even made.

    You can always force your request to be syncronous with async: false, but that's usually not a good idea because it will cause the browser to lock up while it waits for the results.

    The standard way to get around this is by using a callback function.

    function ajax_test(str1, callback){  
       jq.ajax({ 
         //... your options
         success: function(data, status, xhr){  
           callback(data);
         }
       });  
    }  
    

    and then you can call it like this:

    ajax_test("str", function(url) {
      //do something with url
    });
    

提交回复
热议问题