如何通过ajax jquery访问基于java的restful服务

时间:2022-08-26 20:19:35

I am new to jquery. I cannot access java based web services by making ajax call.

我是jquery的新手。我无法通过进行ajax调用来访问基于Java的Web服务。

My code for ajax call is something like this :

我的ajax调用代码是这样的:

$.ajax({
        type: "POST",
        url: url,
        data: parameters,
        contentType: 'application/json; charset=utf-8',
        dataType:"json",
        headers: {
                Accept: "application/json",
                "Access-Control-Allow-Origin": "*"
        },
        success: function(data, textStatus, jqXHR) {
          alert('Success');
        },  
        error: function(jqXHR, textStatus, errorThrown){
          alert('Failure');
        }
});

I am getting error : XMLHttpRequest cannot load "server url. No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'null' is therefore not allowed access.

我收到错误:XMLHttpRequest无法加载“服务器URL。请求的资源上没有'Access-Control-Allow-Origin'标头。因此不允许原点'null'访问。

Web service response while accessing via rest client on browser is something lie this : Status Code: 200 OK Content-Type: application/json Date: Wed, 03 Sep 2014 09:42:32 GMT Server: Apache-Coyote/1.1 Transfer-Encoding: chunke " Any help is greatly appreciated Thanks in advance

在浏览器上通过rest客户端访问时的Web服务响应是这样的:状态代码:200 OK Content-Type:application / json日期:Wed,03 Sep 2014 09:42:32 GMT Server:Apache-Coyote / 1.1 Transfer-Encoding :chunke“非常感谢任何帮助,在此先感谢

2 个解决方案

#1


0  

You trying to access different domain data.

您尝试访问不同的域数据。

So replace with dataType:"jsonp" instead ofdataType:"json",

所以用dataType替换:“jsonp”而不是dataType:“json”,

#2


0  

the Header

 "Access-Control-Allow-Origin": "*"

must be delivered by the server

必须由服务器提供

http://enable-cors.org/server.html

#1


0  

You trying to access different domain data.

您尝试访问不同的域数据。

So replace with dataType:"jsonp" instead ofdataType:"json",

所以用dataType替换:“jsonp”而不是dataType:“json”,

#2


0  

the Header

 "Access-Control-Allow-Origin": "*"

must be delivered by the server

必须由服务器提供

http://enable-cors.org/server.html