首页 文章

使用jQuery ajax调用Restful Web服务

提问于
浏览
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]

我找到了这篇文章并完全遵循了它的作用,但是没有用 . 谁能告诉我为什么?谢谢

Calling Rest webservice using JQuery Ajax call , web service is returning JSON string

1 回答

  • 4

    Headers 必须在服务端 . 因此浏览器知道服务器允许该网站使用Ajax来加载数据 . 所以这些 Headers :

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

    链接:

相关问题