get returned values from jquery get?

前端 未结 3 1509
时光取名叫无心
时光取名叫无心 2021-01-07 10:27

I am making jquery calls to get and getJSON, but cannot access returned values outside of callback function. How can I access the returned data?

        var          


        
3条回答
  •  佛祖请我去吃肉
    2021-01-07 11:10

    ALL AJAX CALLS ARE ASYNCHRONOUS

    SO you need to use callbacks. anything outside that will return undefined.

    $.get(callUrl, function(data) {  // call to add node
         var n = data.indexOf("id-");
         var m = data.indexOf("id-");
         firstid = data.substr(n+3,m - (n+3));
         nextid = data.substr(m+3);
    
         doSomethingWithFirst(firstid);
    });
    
    function doSomethingWithFirst(f)   {
         //NOW do something
    }
    

提交回复
热议问题