Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/340.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/79.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 使用jQuery访问基于Jersey的RESTful服务_Java_Jquery_Jersey - Fatal编程技术网

Java 使用jQuery访问基于Jersey的RESTful服务

Java 使用jQuery访问基于Jersey的RESTful服务,java,jquery,jersey,Java,Jquery,Jersey,我正在尝试访问RESTful服务,该服务是在Java上创建的,在Jersey的帮助下使用jQuery部署的 如果我使用浏览器访问它,我将得到结果,但是从jQuery,我得到一个错误,在页面上看不到任何结果 带有脚本的页面托管在本地Apache服务器上,该服务在同一台机器上使用Jersey/Grizzly单独运行 我可以看到服务正在发送响应,它有200个代码,但我不断从.ajax中得到错误,没有任何细节和细节 有什么问题吗 服务: @Path("/helloworld") 公共类HelloWor

我正在尝试访问RESTful服务,该服务是在Java上创建的,在Jersey的帮助下使用jQuery部署的

如果我使用浏览器访问它,我将得到结果,但是从jQuery,我得到一个错误,在页面上看不到任何结果

带有脚本的页面托管在本地Apache服务器上,该服务在同一台机器上使用Jersey/Grizzly单独运行

我可以看到服务正在发送响应,它有200个代码,但我不断从.ajax中得到错误,没有任何细节和细节 有什么问题吗

服务:

@Path("/helloworld")
公共类HelloWorldResource{

@GET
@Produces
public String test(){
    System.out.println("Sending response");
    return "test";
}
}

主要内容:


您必须将
crossDomain
设置为true才能进行跨域请求

var serviceAddress = "http://192.168.1.2:9998/helloworld";
        function loadDeviceData(){
            $.ajax({
                dataType:'html',
                type:'GET',
                crossDomain:true,
                cache:false,
                async:false,                    
                url: serviceAddress,
                success: function (data) {
                    alert("Data loaded: " + data);
                },
                error: function (xhr) {
                    alert(xhr.responseText + ' ' + xhr.status + ' ' + xhr.statusText);
                }
            });
        }
更新

如果您的服务需要身份验证,您可以这样做

var serviceAddress = "http://192.168.1.2:9998/helloworld";
            function loadDeviceData(){
                $.ajax({
                    dataType:'html',
                    type:'GET',
                    crossDomain:true,
                    cache:false,
                    async:false,
                    xhrFields: {
                      withCredentials: true
                    },
                    username:'yourUsername', //not sure about the username and password options but you can try with or without them
                    password:'thePass',                    
                    url: serviceAddress,
                    success: function (data) {
                        alert("Data loaded: " + data);
                    },
                    error: function (xhr) {
                        alert(xhr.responseText + ' ' + xhr.status + ' ' + xhr.statusText);
                    }
                });
            }

还可以使用jquery 1.5.1或更高版本,因为
跨域
和一些其他选项在早期版本中不可用。参考请参见此链接

经过几天的研究和实验,我发现问题出在响应的标题中。为了能够使用来自服务的响应,我添加了自定义标题字段:

“访问控制允许源:*”

新服务如下所示:

@Path("/helloworld")
public class HelloWorldResource {


    @GET
    @Produces
    public Response test(){

        return Response.ok("test").header("Access-Control-Allow-Origin", "*").build();
    }
}

如果您使用javascript函数来替换典型的表单提交,那么请注意您应该返回false;在javascript函数的末尾

您可以在此处查看类似问题


实际上,这不是一件球衣的问题,而是一个javascript的问题

感谢您的快速响应!但它并没有解决我的问题,我仍然无法获取数据并获得警报“0错误”…它的
数据类型
数据类型
也将
异步
设置为false和
缓存
false。。。查看我编辑过的答案,同时将
dataType
设置为htmlI我已经尝试过了,现在xhr.responseText变为“未定义”,但仍然没有结果…您使用的是什么版本的jquery?确认另一件事您尝试访问的url需要身份验证吗?
var serviceAddress = "http://192.168.1.2:9998/helloworld";
            function loadDeviceData(){
                $.ajax({
                    dataType:'html',
                    type:'GET',
                    crossDomain:true,
                    cache:false,
                    async:false,
                    xhrFields: {
                      withCredentials: true
                    },
                    username:'yourUsername', //not sure about the username and password options but you can try with or without them
                    password:'thePass',                    
                    url: serviceAddress,
                    success: function (data) {
                        alert("Data loaded: " + data);
                    },
                    error: function (xhr) {
                        alert(xhr.responseText + ' ' + xhr.status + ' ' + xhr.statusText);
                    }
                });
            }
@Path("/helloworld")
public class HelloWorldResource {


    @GET
    @Produces
    public Response test(){

        return Response.ok("test").header("Access-Control-Allow-Origin", "*").build();
    }
}