我正在运行本地 VM 进行开发。它使用 php 5.4 和 nginx。当我在backend.dev开发 laravel 后端 api和在 frontend.dev 开发前端 ember 应用程序时,我已将 nginx 配置为允许跨源请求。
我遇到的问题是从我的后端返回 400 状态代码时。其他状态代码工作得很好:201、404、304。由于某种原因,当我返回 400 时,jQuery 只是取消了。chrome 调试器只是说“已取消”,没有任何响应。
我对我的 laravel 后端进行了硬编码,以使用这两个红色 POST 返回 400。对完全相同的端点的所有请求都发布了完全相同的 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 配置文件
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;
}
}
这是我的余烬控制器
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 响应。