0

.json ファイルからデータを取得しようとしていますが、jquery を使用してデータを取得しません。

json ファイルのデータ:

[
{"optiontext" : "One", "optionvalue" : "One"},
{"optiontext" : "Two", "optionvalue" : "Two"},
{"optiontext" : "Three", "optionvalue" : "Three"}
]

このデータにアクセスしようとしているコードは次のとおりです。

<!doctype html>
<html>
    <head> 
      <script type="text/javascript" src="jquery.min.js"></script>
    <script type="text/javascript"> 

    $(document).ready(function() {
            $('#submit').click(function () {
        $.ajax({

    //alert("I'm doing it now");
    //define the attributes for the connection
    type:"GET",
    url:"sample1.json",
    dataType:"json",
    //extrat the data

    success: function (data) {      
    var SampleFileMessage="<ul>";
    $.each(data, function(i,n){
    SampleFileMessage+="<li>"+n["optiontext"]+"</li>";
            });
    SampleFileMessage+="</ul>";
    $('#message').append(SampleFileMessage);
    document.writeln(SampleFileMessage);
}
});
return false;
});
});


    </script>
    </head>
    <body>
        <input type="button" onclick="getjson()" value="pressme"         style="align:center" />    
        <div id="messege"   > <input type="button" id="submit" value=" get the     data from json" ></div>
    </body>
    </html>

このjson形式に対処するにはどうすればよいですか!!!

4

1 に答える 1

0

これを試して:

$.each(data, function(i, n) {
    SampleFileMessage += "<li>" + n.optiontext + "</li>";
});
于 2012-06-15T01:33:39.270 に答える