1

我在 Java 的 localhost 上创建了一个 RESTful Web 服务。当我在导航器地址栏中键入以下网址时:

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

它会给我:

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

现在我想使用 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>

但是我总是收到错误消息:

error: [object Object]

我找到了这篇文章并完全按照它的作用,但不起作用。谁能告诉我为什么?谢谢

使用 JQuery Ajax 调用调用 Rest Web 服务,Web 服务返回 JSON 字符串

4

1 回答 1

4

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. So these headers:

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

Links:

于 2013-04-22T16:56:43.337 回答