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 回答