简体   繁体   English

我应该通过PUT时通过选项

[英]Passing OPTIONS when I should Pass PUT

I have the following configuration. 我有以下配置。

.config(config);

config.$inject = ["$httpProvider"];

function config($httpProvider) {
  $httpProvider.defaults.useXDomain = true;
  delete $httpProvider.defaults.headers.common["X-Requested-With"];


  //Reset headers to avoid OPTIONS request (aka preflight)
  $httpProvider.defaults.headers.common = {};
  $httpProvider.defaults.headers.post = {};
  $httpProvider.defaults.headers.put = {};
  $httpProvider.defaults.headers.patch = {};
}

As for my $resource 至于我的$resource

.factory('Order', order)

order.$inject = ['$resource', "ApiEndpoint", "UserRecord"];

function order($resource, ApiEndpoint, UserRecord) {
  var id = null;

  UserRecord.retrieve().then(function (user) {
    id = user.user_id;
  })

  return $resource(ApiEndpoint.url + 'orders.json', {}, {
    update: {method: 'PUT', url: ApiEndpoint.url + 'orders.json'}
  });
}

Now when I call update like so 现在,当我这样打电话给更新时

var params = { name: "new name" };

Order.update(params, {}, function (resp) {
  console.log(resp);
})

My console renders No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http://localhost:8100' is therefore not allowed access. The response had HTTP status code 401. 我的控制台呈现No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http://localhost:8100' is therefore not allowed access. The response had HTTP status code 401. No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http://localhost:8100' is therefore not allowed access. The response had HTTP status code 401. No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http://localhost:8100' is therefore not allowed access. The response had HTTP status code 401. as an OPTIONS request. No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http://localhost:8100' is therefore not allowed access. The response had HTTP status code 401.作为OPTIONS请求。 If I run a POST request, everything works fine and as intended. 如果我运行POST请求,一切都会按预期进行。

When I visit the server logs it verifies I'm running an OPTIONS request. 当我访问服务器日志时,它会验证我是否正在运行OPTIONS请求。 How do I fix this to run a PUT request? 如何解决此问题以运行PUT请求?

You are trying to make a CORS (Cross Origin Resource Sharing) request, which means that you try to connect to a server that is not your origin. 您正在尝试发出CORS(跨源资源共享)请求,这意味着您尝试连接到非源服务器。 Your origin is the server that serve your application. 您的来源是为您的应用程序提供服务的服务器。

Due to browser security policy, every request other than GET, requires a preflight handshake (OPTIONS), in which the non origin server, describes what headers, and methods he allows someone coming from origin X. 由于浏览器的安全政策,除GET之外,每个请求都需要进行预检握手(OPTIONS),其中非源服务器描述了允许他来自源X的某人使用的标头和方法。

So the only way to skip preflight, is putting the resources you are accessing on the same server from which you serve the application. 因此,跳过预检的唯一方法是将要访问的资源放在为应用程序提供服务的同一服务器上。

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

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