我正在运行本地VM进行开发。它使用php 5.4和nginx。我已经配置了nginx以允许交叉原始请求,因为我正在 backend.dev 开发laravel后端api,并在 frontend.dev 开发前端ember应用程序。
我遇到的问题是从我的后端返回400状态代码。其他状态代码工作得很好:201,404,304。出于某种原因,当我返回400时,jQuery只是取消。铬调试器只是说“已取消”,没有响应。
我硬编码了我的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);
}
}
这是我的vhost的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;
}
}
这是我的余烬控制器
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响应。