简体   繁体   English

如何通过Ajax jQuery访问基于Java的Restful服务

[英]How to access java based restful service through ajax jquery

I am new to jquery. 我是jquery新手。 I cannot access java based web services by making ajax call. 我无法通过进行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内容类型:application / json日期:2014年9月3日星期三09:42:32 GMT服务器:Apache-Coyote / 1.1传输编码:chunke“非常感谢您的帮助

You trying to access different domain data. 您试图访问其他域数据。

So replace with dataType:"jsonp" instead of dataType:"json", 因此,以dataType:"jsonp" instead of dataType:“ json”,

the Header 标头

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

must be delivered by the server 必须由服务器交付

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM