无法从Web服务获得有效响应

时间:2011-03-09 11:28:38

标签: jquery ajax json rest

我目前正在开发一个Web服务的Web前端。我使用jQuery 1.5.1作为JS-Framework和Firebug进行调试。

首先,我使用Firefox扩展“REST Client”测试了Web服务。 在那里,以下请求成功:

{"requestUrl":"http://localhost:8080/Foobar/rest/goods?label=Schro","requestMethod":"GET","requestBody":"","headers":["accept","application/json"]}

现在,我想在jQuery UI autocomplete()方法的上下文中重现此请求:

$("#autocompleteTest").autocomplete({
    source: function( request, response ) {
        $.ajax({
            url: "http://localhost:8080/Foobar/rest/goods/",
            dataType: "json",
            data: {
                label: $("#autocompleteTest").val()
            },
            headers: {'accept':'application/json'},
            error: function(jqXHR, textStatus, errorThrown) {
                log(textStatus + "," + errorThrown);
                log(jqXHR);
            },
            success: function( data ) {
                console.log("bla:" + data);
                response( $.map( data, function( item ) {
                    return {
                        label: item.label + " # " + item.id,
                        value: item.id
                    }
                }));
            }               
        });
    },
       // [...]

我从jQuery UI自动完成demo

中获取了此配方

然而,这会触发错误事件。萤火虫说:

GET http://localhost:8080/Foobar/rest/goods/?label=Schrott - 200 OK 16ms
Antwort-Header
Server  Foobar-Optimizer
Content-Type    application/json;charset=UTF-8
Transfer-Encoding   chunked
Date    Wed, 09 Mar 2011 10:50:03 GMT
Anfrage-Header
Host    localhost:8080
User-Agent  Mozilla/5.0 (Windows NT 6.1; WOW64; rv:2.0b12) Gecko/20100101 Firefox/4.0b12
Accept  application/json
Accept-Language de-de,de;q=0.8,en-us;q=0.5,en;q=0.3
Accept-Encoding gzip, deflate
Accept-Charset  ISO-8859-1,utf-8;q=0.7,*;q=0.7
Keep-Alive  115
Connection  keep-alive
Origin  null

错误jqXHR说:

[Object { readyState=0, status=0, statusText="error"}]

并且响应标头为空。

除了不同的结果,这两个请求在我看来都是一样的。 我将不胜感激任何建议。谢谢。

更新:解决方案

这是一个跨域政策问题。我不知道这种行为。解决方案是将$ .ajax数据类型从“json”更改为“jsonp”。这是一种优雅的方法,可以覆盖这些策略并访问远程Web服务。

当然,Web服务必须能够处理这个json“padding”。

这里提到了许多其他可能性:http://usejquery.com/posts/9/the-jquery-cross-domain-ajax-guide

1 个答案:

答案 0 :(得分:0)

我可以自己回答这个问题:这是一个跨域安全策略问题。我不知道这种行为。

以下链接包含一系列适用的方法:

http://usejquery.com/posts/9/the-jquery-cross-domain-ajax-guide

相关问题