2016-02-04 135 views
0

跨域POST請求不能正常工作,跨域POST請求沒有工作

AngularJS POST請求Drupal是不工作

錯誤:選項的詳細信息: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 

回答

0

看看this ,我看到你的請求和響應,你的選擇的反應有一些缺失的變量,將它添加到您的服務器響應,對我來說它工作正常。

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

,並意識到,你的選擇要求必須返回的StatusCode 200及以上的參數,可以使Ajax請求允許發送POST請求

+0

@Sudhakar你設置的選項方法返回200這個要求嗎? ??它不是默認的。發佈請求首先發送選項請求。也許這種方法由drupal阻止 – Shayan