简体   繁体   English

即使存在所有CORS标头,跨源资源共享问题

[英]Cross Origin Resource sharing issue even when all the CORS headers are present

even though i have appended my service response with following provided CORS Headers : 即使我在服务响应中附加了以下提供的CORS标头

resp.setContentType("application/json");
resp.addHeader("Access-Control-Allow-Origin", "*");
resp.addHeader("Access-Control-Allow-Credentials", "true");
resp.addHeader("Access-Control-Allow-Methods", "GET,POST,OPTIONS");
resp.addHeader("Access-Control-Allow-Headers", "Origin,accept,content-type");
resp.flushBuffer();

i am still getting below error in the console while trying to access some of the POST web methods in the service through my AngularJS frontend. 尝试通过AngularJS前端访问服务中的某些POST Web方法时,控制台中仍然出现错误。

XMLHttpRequest cannot load http://192.***.*.***:8080/abc/def/search/vehicleManufacturer. No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http://192.***.*.***:8085' is therefore not allowed access.

However within the same class, some POST methods without any payloads are responding perfectly. 但是,在同一类中,一些没有任何有效负载的POST方法都可以完美地响应。 Any suggestions ? 有什么建议么 ?

EDIT---------> 编辑--------->

Below is my AngularJS client screen code for calling the web method:- 下面是我的AngularJS客户端屏幕代码,用于调用Web方法:-

getVehicleModel : function(searchData,$scope){
     $http({
         method:'POST',
         url:'http://192.169.*.***:8085/abc/def/search/vehicleModel',
         dataType:'jsonp',
         data:searchData

     }).
     success(function(data){
         console.log("vehicle model")
         $scope.vehicleModel = data.Response;

     });


},

I think the problem here is Preflighted Requests in CORS. 我认为这里的问题是CORS中的预检请求

From the Mozilla docs, 从Mozilla文档中,

Unlike simple requests (discussed above), "preflighted" requests first send an HTTP request by the OPTIONS method to the resource on the other domain, in order to determine whether the actual request is safe to send. 与简单请求(如上所述)不同,“预检”请求首先通过OPTIONS方法向另一个域上的资源发送HTTP请求,以确定实际请求是否可以安全发送。 Cross-site requests are preflighted like this since they may have implications to user data. 跨站点请求这样被预检,因为它们可能会影响用户数据。 In particular, a request is preflighted if: 特别是,在以下情况下,将对请求进行预检:

  • It uses methods other than GET, HEAD or POST. 它使用GET,HEAD或POST以外的方法。 Also, if POST is used to send request data with a Content-Type other than 另外,如果POST用于发送Content-Type以外的请求数据

    • application/x-www-form-urlencoded, application / x-www-form-urlencoded,
    • multipart/form-data 多部分/表单数据
    • text/plain 文字/纯文字

    eg if the POST request sends an XML payload to the server using application/xml or text/xml, then the request is preflighted. 例如,如果POST请求使用application / xml或text / xml将XML有效负载发送到服务器,则该请求被预检。

  • It sets custom headers in the request (eg the request uses a header such as X-PINGOTHER) 它在请求中设置自定义标头(例如,请求使用标头,例如X-PINGOTHER)

As explained above, even though you're making a simple POST request, the Content-Type in your request is application/json which is different from the 3 types mentioned above, so it's considered as a Preflight request and an OPTIONS request is fired before your actual POST request. 如上所述,即使您进行的是简单的POST请求,您请求中的Content-Type application/json ,与上述3种类型不同,因此它被视为Preflight请求,并且在触发OPTIONS请求之前您的实际POST请求。

You can solve this by implementing doOptions in your servlet, just add the headers there and it will work :) 您可以通过在servlet中实现doOptions来解决此问题,只需在其中添加标头即可使用:)

The preflight (OPTIONS) is occurring due to the fact that you are sending a cross-origin ajax request AND specifying an Authorization header with this GET request. 由于您正在发送跨域ajax请求并为此GET请求指定一个Authorization标头,因此发生了预检(OPTIONS)。

Also (this is not causing an issue) I would suggest removing the contentType option. 另外(这不会引起问题),我建议删除contentType选项。 This doesn't make sense in the context of a GET request. 在GET请求的上下文中,这没有任何意义。 A GET request should not have any content. GET请求不应包含任何内容。 All data should be included in the query string or, possibly, headers. 所有数据都应包含在查询字符串或标题中。

The Authorization header will not be sent with the OPTIONS. Authorization标头不会与OPTIONS一起发送。 You must acknowledge it server-side, and then the browser will send the underlying GET. 您必须在服务器端确认它,然后浏览器将发送基础GET。 Read more about CORS at https://developer.mozilla.org/en-US/docs/HTTP/Access_control_CORS . https://developer.mozilla.org/zh-CN/docs/HTTP/Access_control_CORS上了解有关CORS的更多信息。

暂无
暂无

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

相关问题 在Resteasy + Netty服务器上实现跨源资源共享(CORS) - Implement Cross Origin Resource Sharing (CORS) on Resteasy + Netty server Checkmarx 问题 - Spring 过于宽松的跨源资源共享策略 - Checkmarx issue - Spring overly permissive cross origin resource sharing policy 与Servlet Filter和FilterRegistrationBean一起使用时,跨源资源共享不起作用 - Cross Origin Resource Sharing not working when used with Servlet Filter and FilterRegistrationBean 如何使用Spring REST在跨源资源共享(CORS)中接收JSON数据 - How To Receive JSON Data In Cross Origin Resource Sharing (CORS) with Spring REST 正确配置 vert.x 服务器以允许跨域资源共享 (CORS) - Correctly configure vert.x server to allow Cross-origin resource sharing (CORS) Spring Security的跨源资源共享 - Cross-Origin Resource Sharing with Spring Security 具有可配置来源列表的骆驼CXFRS跨来源资源共享过滤器 - Camel CXFRS cross origin resource sharing filter with Configurable Origin List CORS 问题 / akka-http-cors / No 'Access-Control-Allow-Origin' header 出现在 Scala/Java 中请求的资源上 - CORS issue / akka-http-cors / No 'Access-Control-Allow-Origin' header is present on the requested resource in Scala/Java XMLHttpRequest跨域资源共享(qooxdoo和tomcat) - XMLHttpRequest cross-origin resource sharing (qooxdoo and tomcat) Spring 跨域资源共享:预检响应不成功 - Spring Cross Origin Resource Sharing : Preflight response is not successful
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM