開発用のローカル VM を実行しています。nginxでphp 5.4を使用しています。backend.dev でlaravelバックエンド API を開発し、 frontend.devでフロントエンド ember アプリを開発しているため、クロス オリジン リクエストを許可するように nginx を構成しました。
私が直面している問題は、バックエンドから 400 ステータス コードが返されたときです。他のステータス コードは問題なく動作します: 201、404、304。何らかの理由で、400 を返すと jQuery がキャンセルされます。Chrome デバッガーは「キャンセルされました」とだけ表示し、応答はありません。
これら 2 つの赤い POST で 400 を返すように、laravel バックエンドをハードコーディングしました。まったく同じエンドポイントへのすべてのリクエストに対して、まったく同じ json 本文が投稿されました。
class CompanyController extends \BaseController
{
public function store()
{
$json = new stdClass;
$json->code = 400;
$json->message = 'Invalid data';
return Response::json($json, $json->code);
}
}
仮想ホスト用のnginx confファイルは次のとおりです
server {
listen *:80 ;
server_name backend.dev;
access_log /var/log/nginx/backend.dev.com.access.log;
location / {
root /var/www/backend/public;
try_files $uri $uri/ /index.php?$args ;
index index.html index.htm index.php;
}
location ~ \.php$ {
if ($request_method = 'OPTIONS') {
add_header 'Access-Control-Allow-Origin' '*';
#
# Om nom nom cookies
#
add_header 'Access-Control-Allow-Credentials' 'true';
add_header 'Access-Control-Allow-Methods' 'GET, POST, OPTIONS';
#
# Custom headers and headers various browsers *should* be OK with but aren't
#
add_header 'Access-Control-Allow-Headers' 'DNT,X-Mx-ReqToken,Keep-Alive,User-Agent,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type';
#
# Tell client that this pre-flight info is valid for 20 days
#
add_header 'Access-Control-Max-Age' 1728000;
add_header 'Content-Type' 'text/plain charset=UTF-8';
add_header 'Content-Length' 0;
return 204;
}
if ($request_method = 'POST') {
add_header 'Access-Control-Allow-Origin' '*';
add_header 'Access-Control-Allow-Credentials' 'true';
add_header 'Access-Control-Allow-Methods' 'GET, POST, OPTIONS';
add_header 'Access-Control-Allow-Headers' 'DNT,X-Mx-ReqToken,Keep-Alive,User-Agent,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type';
}
if ($request_method = 'GET') {
add_header 'Access-Control-Allow-Origin' '*';
add_header 'Access-Control-Allow-Credentials' 'true';
add_header 'Access-Control-Allow-Methods' 'GET, POST, OPTIONS';
add_header 'Access-Control-Allow-Headers' 'DNT,X-Mx-ReqToken,Keep-Alive,User-Agent,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type';
}
root /var/www/backend/public;
try_files $uri $uri/ /index.php?$args ;
index index.html index.htm index.php;
fastcgi_index index.php;
fastcgi_param PATH_INFO $fastcgi_path_info;
fastcgi_param PATH_TRANSLATED $document_root$fastcgi_path_info;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
fastcgi_param APP_ENV dev;
fastcgi_param APP_DBG true;
fastcgi_pass unix:/var/run/php5-fpm.sock;
fastcgi_split_path_info ^(.+\.php)(/.+)$;
include fastcgi_params;
}
}
これが私のemberコントローラーです
App.PeopleNewController = Ember.ObjectController.extend({
content: Ember.Object.create(),
firstName: '',
lastName: '',
city: '',
state: '',
companyName: '',
email: '',
actions: {
doneEditing: function () {
var firstName = this.get('firstName');
if (!firstName.trim()) {
return;
}
var lastName = this.get('lastName');
if (!lastName.trim()) {
return;
}
var city = this.get('city');
if (!city.trim()) {
return;
}
var state = this.get('state');
if (!state.trim()) {
return;
}
var email = this.get('email');
if (!email.trim()) {
return;
}
// Create the new person model
var person = this.store.createRecord('person', {
firstName: firstName,
lastName: lastName,
city: city,
state: state,
email: email
});
// Clear the fields
this.set('firstName', '');
this.set('lastName', '');
this.set('city', '');
this.set('state', '');
this.set('email', '');
//this.set('companyName', '');
// Save the new model
person.save();
}
}
});
残りのクライアントで問題なく応答が得られます
何が原因なのかわかりません。400 ステータス コードで json レスポンスを取得したいと思います。