使用jQuery使用Java RESTful Web服务

使用jQuery使用Java RESTful Web服务,java,jquery,json,web-services,Java,Jquery,Json,Web Services,使用jQuery使用RESTful Web服务是我想要实现的。在成功地遵循这一点之后。其中一种文件类型是JSON文件。我想从JSON文件中读取要使用jQuery显示在HTML文件中的值。这是迄今为止为测试这一点而编写的代码,但它没有给出正确的输出,我能做什么 这是jQuery文件 $(document).ready(function() { $.ajax({ url: "http://localhost:8080/WMWebServiceApplication/webre

使用jQuery使用RESTful Web服务是我想要实现的。在成功地遵循这一点之后。其中一种文件类型是JSON文件。我想从JSON文件中读取要使用jQuery显示在HTML文件中的值。这是迄今为止为测试这一点而编写的代码,但它没有给出正确的输出,我能做什么

这是jQuery文件

$(document).ready(function() {
    $.ajax({
        url: "http://localhost:8080/WMWebServiceApplication/webresources/com.mycompany.wmwebserviceapplication"
    }).then(function(data) {
       $('.discountCode').append(data.discountCode);
       $('.rate').append(data.rate);
    });
});
这些是使用Java创建的Web服务的参数

URL: http://localhost:8080/WMWebServiceApplication/webresources/com.mycompany.wmwebserviceapplication.discountcode
JSON参数和值

[{"discountCode":"H","rate":16.00},{"discountCode":"M","rate":11.00},{"discountCode":"L","rate":7.00},{"discountCode":"N","rate":0.00}]
请求方法

![GET(application/json)][3]

这是因为您正在接收JSON中的一个对象数组,因此,例如,如果您想访问第一个元素,您应该编写:

$(document).ready(function() {
    $.ajax({
        url: "http://localhost:8080/WMWebServiceApplication/webresources/com.mycompany.wmwebserviceapplication"
    }).then(function(data) {
        $('.discountCode').append(data[0].discountCode);
        $('.rate').append(data[0].rate);
    });
});

下面是一种循环数据并检索所有值的方法-

for(i=0;i');
}

如果您还没有完成。去获取Firebug,或者使用Chrome的开发工具,在收到数据时设置一个断点并检查得到的数据。
for(i = 0; i < data.length; i++) {
    $('.discountCode').append(data[i].discountCode + ' ' + data[i].rate + '<br />');
}