1

jqueryとJSONは初めてです。私は次のJSON構造を持っています。

{
   "address":{
      "suburb":[
         "HACKHAM WEST",
         "HUNTFIELD HEIGHTS",
         "ONKAPARINGA HILLS",
         "m"
      ],
      "state":"SA"
   }
}

したがって、基本的に上記はこれからの応答です。

$.ajax({
    type:'POST',
    url: 'getAddress.php',
    data:postCode='+postCode',
    success: function(response) {
        alert(response)
    }
});

したがって、私が取得しようとしているのは、状態を含む変数と、郊外を含む配列です。

4

3 に答える 3

5

有効なAjaxリクエストがあることを確認してください:

$.ajax({
    type: "POST",
    url: "getAddress.php",
    data: {
        postCode : postCode          // data as object is preferrable
    },
    dataType: "json",                // to parse response as JSON automatically
    success: function(response) {
        var state = response.address.state;
        var suburbs = response.address.suburb;
    }
});
于 2012-06-26T17:43:59.873 に答える
3

これはトリックを行う必要があります

$.ajax({type:'POST', 
    url: 'getAddress.php', 
    dataType: 'json',
    data:'postCode='+postCode, 
    success: function(response) {
        var state = response.address.state;
        var suburbs = response.address.suburb;   
    }
});

パラメータを追加dataType:'json'して修正しました。data

于 2012-06-26T17:43:14.383 に答える
1

取得したJSONを解析する必要があります。 $.ajaxあなたのためにこれを行うことができます。

$.ajax({
    type:'POST',
    url: 'getAddress.php',
    data: 'postCode='+postCode, // make sure this line is correct
    dataType: 'json', // this tells jQuery to parse it for you
    success: function(response) {
        var state = response.address.state;
        var suburbs = response.address.suburb;
    }
});
于 2012-06-26T17:44:44.207 に答える