简体   繁体   English

Angular http post请求 - 请求的资源上不存在“Access-Control-Allow-Origin”标头

[英]Angular http post request - No 'Access-Control-Allow-Origin' header is present on the requested resource

I am trying to send data to servlet using angular http post, 我正在尝试使用angular http post将数据发送到servlet,

var httpPostData = function (postparameters,postData){

     var headers = {
                'Access-Control-Allow-Origin' : '*',
                'Access-Control-Allow-Methods' : 'POST, GET, OPTIONS',
                'Accept': 'application/json'
            };
  return $http ({

    method  : 'POST',
    url     : 'http://localhost:8080/json/jasoncontroller',
    params  : postparameters,
    headers: headers,
    data    : postData
   }).success (function (responseData){
         return responseData.data;
   })
}

But i am keep on getting error that No 'Access-Control-Allow-Origin' header is present on the requested resource. 但我继续得到错误, 请求资源上没有'Access-Control-Allow-Origin'标头。 Origin 'null' is therefore not allowed access. 因此不允许原点'null'访问。

I did have set following headers on my servlet 我确实在我的servlet上设置了以下标题

    response.addHeader("Access-Control-Allow-Origin", "*");
    response.addHeader("Access-Control-Allow-Methods", "POST, GET, PUT, OPTIONS, DELETE");
    response.addHeader("Access-Control-Max-Age", "3600");
    response.addHeader("Access-Control-Allow-Headers", "Origin, X-Requested-With, Content-Type, Accept");

if i remove data from http post it works fine, but no luck with data. 如果我从http帖子中删除数据它工作正常,但数据没有运气。

在此输入图像描述

Actually what happens is in some frameworks two calls are made, 实际上在一些框架中发生了两次调用,

  • OPTIONS which checks what methods are available, 用于检查可用方法的选项,
  • And then there is the actual call. 然后有实际的电话。

OPTIONS require just empty answer 200 OK 选项只需要空答案200 OK

if(request.methord == 'OPTIONS'){
  res.send(200);
  } else {
   next();
  }

You can also install this chrome extension to enable cors, that is the easy way out ! 你也可以安装这个chrome扩展来启用cors,这是最简单的方法!

暂无
暂无

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

相关问题 角度4-所请求的资源上没有“ Access-Control-Allow-Origin”标头 - Angular 4 - No 'Access-Control-Allow-Origin' header is present on the requested resource 所请求的资源上不存在“ Access-Control-Allow-Origin”标头-Angular 5 - No 'Access-Control-Allow-Origin' header is present on the requested resource - Angular 5 Web API的$ http角向给出XMLHttpRequest错误-请求的资源上没有'Access-Control-Allow-Origin'标头 - Angular $http to Web Api gives error of XMLHttpRequest -No 'Access-Control-Allow-Origin' header is present on the requested resource 无法在 HTML JS 中发布请求 | 请求的资源上不存在“Access-Control-Allow-Origin”header - Can not post request in HTML JS | No 'Access-Control-Allow-Origin' header is present on the requested resource 成功的POST请求后引发“在请求的资源上不存在'Access-Control-Allow-Origin'标头”错误 - “No 'Access-Control-Allow-Origin' header is present on the requested resource” error thrown after successful POST request 所请求的资源上没有“ Access-Control-Allow-Origin”标头。 因此,不允许访问来源“ http:// localhost” - No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http://localhost' is therefore not allowed access 请求的资源上不存在“Access-Control-Allow-Origin”标头。 起源'http://fiddle.jshell.net' - No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http://fiddle.jshell.net' Javascript-所请求的资源上没有“ Access-Control-Allow-Origin”标头 - Javascript - No 'Access-Control-Allow-Origin' header is present on the requested resource 请求的资源上不存在“Access-Control-Allow-Origin”标头 - No 'Access-Control-Allow-Origin' header is present on the requested resource NodeJ在所请求的资源上不存在“ Access-Control-Allow-Origin”标头 - NodeJs No 'Access-Control-Allow-Origin' header is present on the requested resource
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM