跨域POST请求不起作用

时间:2016-02-04 15:43:58

标签: angularjs apache drupal

跨域POST请求无法正常工作,

对Drupal的AngularJS POST请求无法正常工作

错误:选项详细信息:405方法不允许

我的AngularJS代码:

  var settings = {
    "async": true,
    "crossDomain": true,
    "url": "http://d8v3.local/user/data",
    "method": "POST",
    "headers": {
      "content-type": "*",
      "authorization": "Basic "+authdata,
      "cache-control": "no-cache",
    }
  }

    $.ajax(settings).done(function (response) {          
      // my func
    });

我的请求标题:

OPTIONS /user/data HTTP/1.1
Host: d8v3.local
User-Agent: Mozilla/5.0 (Windows NT 10.0; WOW64; rv:44.0) Gecko/20100101 Firefox/44.0
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8
Accept-Language: en-US,en;q=0.5
Accept-Encoding: gzip, deflate
Access-Control-Request-Method: POST
Access-Control-Request-Headers: authorization,cache-control,content-type
Origin: http://headless.local
Connection: keep-alive

响应标题:

HTTP/1.1 405 Method Not Allowed
Date: Thu, 04 Feb 2016 13:45:33 GMT
Server: Apache/2.4.17 (Win64) PHP/7.0.0
x-content-type-options: nosniff, nosniff
x-powered-by: PHP/7.0.0
Cache-Control: must-revalidate, no-cache, post-check=0, pre-check=0, private
Allow: GET, POST
x-ua-compatible: IE=edge
Content-Language: en
X-Frame-Options: SAMEORIGIN
Expires: Sun, 19 Nov 1978 05:00:00 GMT
X-Generator: Drupal 8 (https://www.drupal.org)
Access-Control-Allow-Origin: *
access-control-allow-credentials: true
access-control-allow-methods: GET, PUT, POST, DELETE, OPTIONS
Access-Control-Allow-Headers: DNT,X-Mx-ReqToken,Keep-Alive,User-Agent,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type
Content-Length: 68
Keep-Alive: timeout=5, max=100
Connection: Keep-Alive
Content-Type: text/html; charset=UTF-8

1 个答案:

答案 0 :(得分:0)

看看this 当我看到你的请求和响应时,你的选项响应有一些缺失的变量,将它添加到你的服务器响应中,对我来说它运行正常。

Access-Control-Allow-Headers: Content-Type,Authorization,Cache-Control
Access-Control-Allow-Origin: *
access-control-allow-methods: OPTIONS,GET,POST,PUT,DELETE

并注意,您的OPTIONS请求必须返回statusCode 200及更高版本的params,以便允许ajax请求发送POST请求