本文介绍了如何从JQuery getJSON读取数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在为getJSON苦苦挣扎.我有一个简单的StockWatcher应用程序,它以JSON格式返回数据

I am struggling with getJSON. I have a simple StockWatcher application which returns the data in JSON format

http://localhost:8080/StockWatcherServer/stockwatcher/stockPrices?q=ABC+DEF+PQR

输出:

({
    "stocks": [{
        "symbol": "ABC",
        "price": 80.11611442288577,
        "change": 1.4332410131550721
    }, {
        "symbol": "DEF",
        "price": 89.47611015580729,
        "change": -1.469336678470048
    }, {
        "symbol": "PQR",
        "price": 99.60017237722221,
        "change": -1.3303545392913447
    }]
})

当我使用简单的Javascript函数读取此内容时,出现错误(.error,.complete和.secondcomplete)

When I use a simple Javascript function to read this, I get a Error (.error, .complete and .second complete)

我已经使用Firebug进行了调试,可以看到可以检索该对象,但是看到XML错误

I have used Firebug to debug this, and I can see that I can retrieve the object but I see a XML error

({"stocks": [ ^

这是Javascript.

Here is the Javascript.

<script type="text/javascript">
$(document).ready(function(){
var url='http://localhost:8080/StockWatcherServer/stockwatcher/stockPrices?q=';
var query;
    $('button').click(function(){
        query=$("#query").val();
        // Assign handlers immediately after making the request,
        // and remember the jqxhr object for this request    
        var jqxhr = $.getJSON(url, function(data) {
            var obj = $.parseJSON(data);
            $.each(obj,function(i,item){
                $("#results").append('Title:'+item.symbol+' ==  Price:'+item.price+'</p>');
            });
        })
        .success(function(data) { alert("second success"); })
        .error(function(data) { alert("error"); })
        .complete(function(data) { alert("complete"); });
        // perform other work here ...

        // Set another completion function for the request above
        jqxhr.complete(function(){ alert("second complete"); });       
    });
});
</script>

我尝试了各种调用parseJSON的选项,而没有使用parseJSON,但似乎不起作用.

I have experimented with various options calling parseJSON and without parseJSON,but seems it doesn't work.

推荐答案

我认为您正在寻找类似这样的东西...试试:

I think you are looking for something more like this... Try:

$(document).ready(function(){
var url='http://localhost:8080/StockWatcherServer/stockwatcher/stockPrices?q=';
var query;
    $('button').click(function(){
        query=$("#query").val();

        // Assign handlers immediately after making the request,
        // and remember the jqxhr object for this request    
        $.ajax({
        url : url,
        type: "GET",
        dataType: "json",
        success: function(data) {
            $.each(data.stocks,function(i,item){
                $("#results").append('Title:'+item.symbol+' ==  Price:'+item.price+'</p>');
            });
        },
        error: function(data) { alert("error"); },
        });

        // perform other work here ...
    });
});

这篇关于如何从JQuery getJSON读取数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-19 04:20