在ajax函数调用之后获取变量中的数据 [英] Get data in variable after ajax call of function

查看:111
本文介绍了在ajax函数调用之后获取变量中的数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想进行2-3次ajax调用并检索各自的JSON数据,以便将所有JSON数据合并到一个最终的JSON对象中,然后对最终的JSON对象执行排序。



var json1 = [{countryName:Argentina,countryName:Bolivia}]

var json2 = [{countryName:Australia,countryName :纽埃}]



然后合并它



json1和json2的数据将来从正在进行ajax调用的功能。





可以有人帮助我。



我的代码///

I want to make 2-3 ajax calls and retrive their respective JSON data in order to combine all the JSON Data in one final JSON object then perform sorting on final JSON object.

var json1 = [{"countryName":"Argentina","countryName":"Bolivia"}]
var json2 = [{"countryName":"Australia","countryName":"Niue"}]

then merge it

the data of json1 and json2 will come from the function which is doing ajax call.


Could anybody help me.

my code///

var getCountries1 = function () {
var countries;
        $.ajax({
            url: "http://www.geonames.org/childrenJSON?geonameId=6255150",
            type: "post",
            asyn: true,
            success: function (data) {
                countries = data;
                
            }
        });
return countries;
//this is returning nothing as ajax call are asynchronous
    }

var getCountries2 = function () {
var countries;
        $.ajax({
            url: "http://www.geonames.org/childrenJSON?geonameId=6255151",
            type: "post",
            asyn: true,
            success: function (data) {
                countries = data;
            }
        });
return countries;
//this is returning nothing as ajax call are asynchronous
    }







$(function () {
   var json1 =  getCountries1();
   var json2 =  getCountries2();

});

推荐答案

.ajax({
url: http://www.geonames.org/childrenJSON?geonameId=6255150
类型: post
asyn: true
成功:功能(数据){
countries = data;

}
});
返回个国家/地区;
// 由于ajax调用是异步的,所以没有返回任何内容
}

var getCountries2 = function (){
var 个国家/地区;
.ajax({ url: "http://www.geonames.org/childrenJSON?geonameId=6255150", type: "post", asyn: true, success: function (data) { countries = data; } }); return countries; //this is returning nothing as ajax call are asynchronous } var getCountries2 = function () { var countries;


.ajax({
url: http://www.geonames.org/childrenJSON?geonameId=6255151
类型: post
asyn: true
成功: function (data){
countries = data;
}
});
返回个国家/地区;
// 由于ajax调用是异步的,所以没有返回任何内容
}
.ajax({ url: "http://www.geonames.org/childrenJSON?geonameId=6255151", type: "post", asyn: true, success: function (data) { countries = data; } }); return countries; //this is returning nothing as ajax call are asynchronous }








function (){
var json1 = getCountries1();
var json2 = getCountries2();

});
(function () { var json1 = getCountries1(); var json2 = getCountries2(); });


这篇关于在ajax函数调用之后获取变量中的数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆