简体   繁体   English

使用jQuery ajax调用Restful Web服务

[英]Calling Restful Web service using jQuery ajax

I've made a RESTful web service at localhost in Java. 我在Java的localhost上创建了一个RESTful Web服务。 When I type the following url in my navigator address bar : 当我在导航器地址栏中输入以下网址时:

http://localhost:8080/GeonotesApp2-war/webresources/route/1

It will give me : 它会给我:

{"rid":"1","routeComment":"hhhahahahah","routeDateCreate":"2012-04-03T00:00:00+02:00","routeDistance":"13400.0","routeName":"route1","routeUserId":"1"}

Now I want to use an Ajax call to get this JSON information with this script : 现在我想使用Ajax调用来获取此脚本的JSON信息:

<html >
<head>
    <meta charset="utf-8">
    <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
    <script>
    $(function(){
        $.ajax({
            type: "GET",
            url: "http://localhost:8080/GeonotesApp2-war/webresources/route/1",
            dataType: 'json',
            headers: {
                Accept: "application/json",
                "Access-Control-Allow-Origin": "*"
            },
            success: function(resp) {
                alert("success");
            },
            error: function(e) {
                alert("error: "+e);
            }
        });
    });

</script>   
</head>
<body>
    Hello Test Service!
</body>

However I'm always getting the error message : 但是我总是收到错误消息:

error: [object Object]

I found this post and followed exactly what it does, but doesn't work. 我找到了这篇文章并完全遵循了它的作用,但是没有用。 Can anyone tell me why? 谁能告诉我为什么? Thanks 谢谢

Calling Rest webservice using JQuery Ajax call , web service is returning JSON string 使用JQuery Ajax调用调用Rest Webservice,Web服务返回JSON字符串

The headers have to be on the service side. 标题必须在服务端。 So the browser knows that the server allows this website to use Ajax to load the data. 因此浏览器知道服务器允许该网站使用Ajax来加载数据。 So these headers: 所以这些标题:

Access-Control-Allow-Origin: *
Access-Control-Allow-Methods: GET

Links: 链接:

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM