如何使多个Ajax调用? [英] How to make multiple ajax calls?

查看:134
本文介绍了如何使多个Ajax调用?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有5个下拉框状(国家,州,区,镇,街道)。结果
当我选择的国家,我加载(州,区,镇,街道),属于国家所有。

I have 5 drop-down boxes like(Country,State,District,Town,street).
When i select country,i am loading the (state,district,town,street) belong to the country.

我的问题是现在我这个做4个独立的AJAX调用。

my question is now i am making 4 separate ajax calls for this.

$('#Country').change(function(){
var procemessage = "<option value='0'> Please wait...</option>";
    $("#State").html(procemessage).show();
    var CountryId = $(this).val();
    $.ajax({
        url: '../Home/LoadStateForCountry?CountryId=' + CountryId,
        success: function (result) {
            var markup = "";
            if (result.length < 1) {
                markup = "<option value='0'>--Nothing to Select--</option>";
            } else {
                data = result;
                markup = "<option value='0'>--Select--</option>";
                for (var x = 0; x < data.length; x++) {
                    markup += "<option value=" + data[x].ValueId + ">" + data[x].DisplayText + "</option>";
                }
            }
            $("#State").html(markup).show();
        },
    error: function (result) { }
});
}); 

$('#Country').change(function(){
//Load district
}); 

$('#Country').change(function(){
//Load town
}); 

$('#Country').change(funstion(){
//Load street
}); 

是不是做到这样。或者反正是有一次调用它。结果
我是新来的MVC。所以请指导我。

Is it right to do like this. or is there anyway to call it at once.
I am new to MVC. so please guide me.

推荐答案

您可以简单地使用AJAX调用来加载国家状态

You can simply use ajax call to load country state

$('#Country').change(funstion(){
BindStateDropDown();
BindDistrictDropDown();
BindTownDropDown();
BindStreetDropDown();
}); 

否则你可以做的是呼叫退一万AJAX调用可以使另一个Ajax调用
让说,在LoadState的的回调可以使另一个Ajax调用加载区。

Or else What you can do is on call back of one ajax call you can make another ajax call Let say on call back of LoadState you can make another ajax call to load District.

$.get("URLToLoadState",{CountryId:CountryId <Just a parameter>},function(data){
        BindDistrict()
});

和上再打区,你可以去镇,然后街道。

And on call back of District, you can go for Town and then street.

或另一种选择是,

您可以创建一个包装类为所有这些实体,如,

You can create a wrapper class for all these entities like ,

public class LocationModel
{
  public List<StateModel> StateList{get;set;}
  public List<DistrictModel> StateList{get;set;}
  public List<TownModel> StateList{get;set;}
  public List<StreetModel> StateList{get;set;}
}

和有关国家的变化,你可以在一个单一的呼叫得到这一切的集合

And on change of Country you can get this all the collection in a single call

$('#Country').change(funstion(){
$.get("UrlToLoadLocations",{CountryId:CountryId},function(data){
BindStateDropDown(data.StateList);
BindStateDropDown(data.DistrictList);
BindStateDropDown(data.TownList);
BindStateDropDown(data.StreetList);
});

});

这篇关于如何使多个Ajax调用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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