数据交互 ajax 初始化省

 1 //初始化省
 2 function initProvince() {
 3     if( areaLvel == 0 ) {
 4         return;
 5     }
 6     // 清空option
 7     $("#select_province").html('');
 8     
 9     $.ajax({
10         type: 'GET',
11         url: '/path/to/file',
12         dataType: 'json',
13         contentType: "application/json;charset='UTF-8'",
14         data: {
15             areaLvel: areaLvel 
16         },
17         cache: false,
18         success: function (data) {
19             if( data.code == 0 ){
20                 // 系统异常
21                 
22             } else if (data.code == 1) {
23                 // 添加option
24                 $("#select_province").append($("<option/>").val(0).text("请选择省市"));
25                 if( data.data != null ) {
26                     $.each(function(i, item) {
27                         $("#select_city").append($("<option/>").val(item.code).text(item.name));
28 
29                     });
30                 }
31             } else if (data.code == -1) {
32                 // 未登录
33                 index();
34             } 
35         } // success end
36 
37     }) // ajax end
38 }
 

  

原文地址:https://www.cnblogs.com/zsongs/p/5996118.html