2017-03-09 17 views
1

構造を折りたたむときに問題があります。私は2つのドメイン:example.comapi.example.comを持っています。私は、Angularのexample.comにメインサイトを書いています。私はapi.example.comで関数を呼び出すためにexample.com/app.jsにこのコードを使用します。プリフライトの応答に無効なHTTPステータスコードがあります

$http({ 
    method: 'POST', 
    url: 'http://api.example.com/register', 
    dataType: 'json', 
    crossDomain: true, 
    data: { 
     email: register.data.email, 
     password: register.data.password 
    }, 
    headers: { 
     'Content-Type': 'application/json; charset=UTF-8' 
    } 
}).then(function(data) { 
    console.log(data); 
    }, function(data) { 
     console.log(data); 
}); 

そして、すべてのofthisは私にエラーを与える

OPTIONS http://api.example.com/register 400 (Bad Request) 

XMLHttpRequest cannot load http://api.example.com/register. Response for preflight has invalid HTTP status code 400 

//some logic code here 
if ($json['status'] == 0) 
{ 
    header('Access-Control-Allow-Origin: *'); 
    header('Access-Control-Allow-Methods: POST, GET'); 
    header('Access-Control-Allow-Headers: Content-Type'); 
    header('Content-type: application/json; charset=utf-8'); 
    echo json_encode($json['data']); 
} 
else 
{ 
    header('Access-Control-Allow-Origin: *'); 
    header('Access-Control-Allow-Methods: POST'); 
    header('Access-Control-Allow-Methods: GET'); 
    header('Access-Control-Allow-Headers: X-Requested-With'); 
    header('HTTP/1.1 400 Bad Request'); 
    header('Access-Control-Allow-Headers: Content-Type'); 
    header('Content-Type: application/json; charset=UTF-8'); 
    http_response_code(400); 
} 
をfolowing私のPHPコードのリターンを

ここで私の$json['status']=0はすべてOKです。正常に動作し、200 OKを返します。私が間違ったパラメータを与えると、私は$json['status']=1を持っていると私は説明したエラーが発生します。事実として

Request URL:http://api.example.com/user/register 
Request Method:OPTIONS 
Status Code:400 Bad Request 
Remote Address:78.46.140.200:80 
Response Headers 
view source 
Access-Control-Allow-Headers:X-Requested-With 
Access-Control-Allow-Methods:GET 
Access-Control-Allow-Origin:* 
Connection:close 
Content-Type:text/html; charset=UTF-8 
Date:Thu, 09 Mar 2017 17:12:22 GMT 
Server:Apache 
Transfer-Encoding:chunked 
Request Headers 
view source 
Accept:*/* 
Accept-Encoding:gzip, deflate, sdch 
Accept-Language:ru-RU,ru;q=0.8,en-US;q=0.6,en;q=0.4,az;q=0.2 
Access-Control-Request-Headers:content-type 
Access-Control-Request-Method:POST 
Connection:keep-alive 
Host:api.example.com 
Origin:http://example.com 
Referer:http://example.com/register 
User-Agent:Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/56.0.2924.87 Safari/537.36 
+0

ajaxメソッドが生成するhttpコールデータを提供できましたか? OPTIONSとPOSTの両方を呼び出してください。 –

+0

@Pheagey私は自分のajaxの質問を投稿しました。私はOPTIONSメソッドで呼び出しを行いません。私のPHPファイルがエラー400のヘッダを返すとき、彼は私にこのエラーを与えます。私はPOSTメソッドで常にPHPにデータを送る –

+0

'preflight'はOPTIONSリクエストです。ブラウザでネットワークタブを開き、コードを実行します。 .ajaxメソッドは、要求がクロス・ソース要求(CORS)タイプ・コールであるため、POSTする前にOPTIONS要求を自動的に作成します。 –

答えて

0

を返すネットワーク

フルヘッダ、すべての問題は、角度にありました。 Angularは最初にOPTIONSリクエストを送信します。このリクエストに200 OKヘッダーがある場合は、選択されたリクエストを送信します(私の場合はPOST)。私はすべてのOPTIONSメソッドに対して200 OKヘッダーを作成しました。

関連する問題