2016-12-20 6 views
1

こんにちは、データをドロップダウンにバインドするためにjQuery Ajaxを使用しています。ソースとしてarcgis restサービスを使用しています。私はレスポンスを得ることができますが、ドロップダウンにバインドできません。Jsonpレスポンスを使用してドロップダウンにバインドする方法は?

$.ajax({ 
    url: "url", 
    data: { f: "json", where: "1 = 1 ", returnGeometry: false }, 
    dataType: "jsonp", 
    success: function(response) {  
    console.log(response); 

    var len = response.length; 

      $("#ddlDistrict > option").remove(); 
      $('#ddlDistrict').append("<option value='-- Select --'>-- Select --</option>"); 

      for (var i = 0; i < len; i++) { 
       $('#ddlDistrict').append('<option value="' + response[i].DistrictName + '">' + response[i].DistrictName + '</option>'); 
      } 
    } 
}); 

私のコンソール出力は、あなたがキー名を渡すために'DistrictName'

SOあなたが持っているの価値を持つdisplayFieldNameを表示したいと思われる応答から

enter image description here

+1

応答がオブジェクトであるように思わ変わりますが、あなたは、配列 – Garfield

+0

として反復処理しようとしたしている場合は何が間違っ起こっている、あなたはすべてのエラーのためのコンソールをチェックしました? – Manish

+0

エラーはありません。しかし、response.lengthは未定義です。 – vithika

答えて

1
success: function(response) {  
     console.log(response, typeof response); 
     if(typeof response == 'string') { 
      response = JSON.parse(response); 
     } 
     var len = response.features.length; 

     $("#ddlDistrict > option").remove(); 
     $('#ddlDistrict').append("<option value='-- Select --'>-- Select --</option>"); 

     for (var i = 0; i < len; i++) { 
     console.log(response.features[i]); 
      $('#ddlDistrict').append('<option value="' + response.features[i].DistrictName + '">' + response.features[i].DistrictName + '</option>'); 
     } 

}

+0

こんにちは私はundefined.alert(response.features [i] .DistrictName)を取得しています。 – vithika

+0

それは私のために働いています。私は.... response.features [i] .attributes.DistrictName – vithika

0

ある

for (var i = 0; i < len; i++) { 
       $('#ddlDistrict').append('<option value="' + response[i].displayFieldName+ '">' + response[i].displayFieldName+ '</option>'); 
      } 

今応答から私はdisplayFieldNameがただのキーであることを見る。ここではfeaturesfieldAliasesfieldsは配列であり、オブジェクト&の配列が再びある。

nameをキーとし、DistrictNameを値とするfieldsをターゲットに設定するかどうか不安です。それは、ループ条件がする必要がOであり

for (var i = 0; i < response.fields.length; i++) { 
    $('#ddlDistrict').append('<option value="' + response.fields[i].displayFieldName+ '">' + response.fields[i].name+ '</option>'); 
    } 
+0

ドロップダウンに値が表示されていますが、値は未定義です。 – vithika

+0

オブジェクトキーを確認 – brk

関連する問題