0

私は PHP に精通していますが、AJAX の初心者であり、JSON に夢中になっています。

私の PHP スクリプトは単純な JSON 文字列を提供します。

    {"bindings": [ {"ircEvent": "PRIVMSG", "method": "newURI", "regex": "^http://.*"},
    {"ircEvent": "PRIVMSG", "method": "deleteURI", "regex": "^delete.*"}, 
    {"ircEvent": "PRIVMSG", "method": "randomURI", "regex": "^random.*"} ] } I 

次のように取得します。

var fillDataMasterTable = $.ajax({          
    url: "/php/PrepareGoogleDataTable.php" +"?"+
                "SensorString="+SensorString + "&"+
                "TableType=" + TableType +"&"+
                "NoAbscissaBins=" + NoAbscissaBins + "&"+
                "DataTableName=" + DataTableName +"&"+
                "TimeUnit=" + TimeUnit +"&"+
                "TimeStart=" + TimeStart,          
    dataType:'json',          
    async: false          
    }).responseText;    

 $('#debug_div').html(fillDataMasterTable);

これで問題なく動作し、JSON 文字列が返されました。しかし、どうすれば個々の値にアクセスできますか? 私は試した:

          $('#debug_div').html(fillDataMasterTable.bindings);

しかし、それはうまくいきません。私は確かにここでひどくばかげたことをしており、少し恥ずかしいですが、始めるには手を握る必要があります..

4

2 に答える 2

4

reponseTextはオブジェクトのプロパティでXMLHttpRequestあり、リクエストJSON後のオブジェクトにはStringなりません。"dataType""json"

したがって、それを json オブジェクトに変換する必要があります。

 fillDataMasterTable = JSON.parse(fillDataMasterTable);

次に、次のようなプロパティにアクセスできます。

 fillDataMasterTable.bindings
于 2012-07-06T20:30:10.810 に答える
1

次のように Jquery を使用します。

$.each(fillDataMasterTable.bindings, function(i, object) {
            $.each(object, bindings(property, value) {
                $('#debug_div').html(value);
            });
        });
于 2012-07-06T20:30:04.173 に答える