我在前端有一个简单的ajax请求
$.ajax({
url: '/api/marketing-research',
dataType: 'json',
type: 'post',
data: this.form.data(),
success(resp) {
console.log(resp);
}
});
后端的简单处理程序
public function store(MarketingResearchRequest $request)
{
$research = Auth::user()->addMarketingResearch($request->castAll());
return $this->respond([
'type' => 'store marketing research',
'message' => 'Marketing Research successfully store.',
'id' => $research['id'],
'routeEdit' => route('services.marketingResearch.card', ['marketingResearch' => $research['id']]),
]);
}
如您所见,这里使用了验证请求,所以如果我没有通过验证,我预计代码422和json响应错误消息会出错。 但我得到200 HTTP状态确定与HTML /文本标题和错误。在生产服务器上,它使用相同的代码正常工作。那么,我哪里错了?
MarketingResearchRequest
<?php
namespace App\Http\Requests;
use Illuminate\Foundation\Http\FormRequest;
use Media101\Request\Cast\Castable;
class MarketingResearchRequest extends FormRequest
{
use Castable;
/**
* Determine if the user is authorized to make this request.
*
* @return bool
*/
public function authorize()
{
return true;
}
/**
* Get the validation rules that apply to the request.
*
* @return array
*/
public function rules()
{
return [
'name' => 'required|max:200',
'target' => 'max:200',
'description' => 'string',
'started_at' => 'required|date',
'count_respond' => 'required|integer|min:1',
'price' => 'required|numeric|min:10',
'sum' => 'numeric',
'status' => 'integer',
'striations' => 'array',
'striations.*.fields.',
'striations.*.fields.volume' => 'required|numeric|min:0|max:100|nullable',
'striations.*.fields.gender' => 'string|max:10|nullable',
'striations.*.fields.ageFrom' => 'numeric|min:0|max:150|nullable',
'striations.*.fields.ageOn' => 'numeric|min:0|max:150|nullable',
'striations.*.fields.education' => 'string|max:40|nullable',
'striations.*.fields.ideology' => 'string|max:40|nullable',
'striations.*.fields.family_status' => 'string|max:40|nullable',
'striations.*.fields.children' => 'boolean|nullable',
'striations.*.fields.childrenCount' => 'numeric|max:100|nullable',
'striations.*.fields.incomeFrom' => 'numeric|nullable',
'striations.*.fields.incomeOn' => 'numeric|nullable',
'striations.*.fields.politics' => 'string|max:40|nullable'
];
}
protected function casts()
{
return [
'target' => 'null',
'started_at' => 'null',
'count_respond' => 'null',
'price' => 'null',
'sum' => 'null',
'status' => 'null',
];
}
}
可浇铸特性
<?php
namespace Media101\Request\Cast;
use Carbon\Carbon;
use Illuminate\Http\Request;
use Illuminate\Support\Str;
/**
* Allows request to be able to cast it's parameters to desired types.
*
* @example
*
* // In request model
* public function casts()
* {
* return [
* 'birthed_at' => ['date', 'd.m.Y'],
* 'address' => 'null'
* ];
* }
*
* // In controller
* $model->update($request->castAll());
*
* Available casts:
* * date($format) - Casts to Carbon object using given format
* * null() - Casts empty string to null
*
* @mixin Request
*/
trait Castable
{
/**
* Override this method to define which attributes should this request cast
*
* @return array Indexed by field name, with value being array of cast name and case function arguments
*/
abstract protected function casts();
/**
* Extract all parameters and cast them
*
* @return array
*/
public function castAll()
{
return $this->castData($this->all());
}
/**
* Extract only specified parameter values and cast them
*
* @param sting[] $fields
* @return array
*/
public function castOnly($fields)
{
return $this->castData($this->only($fields));
}
/**
* Extract all the parameters except specified and cast them
*
* @param string[] $fields
* @return array
*/
public function castExcept($fields)
{
return $this->castData($this->except($fields));
}
/**
* Extract subarray from field and casts all the values there
*
* @param string $field
* @return array
*/
public function castNested($field)
{
return $this->castData($this->input($field, []));
}
/**
* Extract input value and casts it
*
* @param string $field
* @return mixed
*/
public function castInput($field, $default = null)
{
$casts = $this->casts();
return isset($casts[$field]) && $this->exists($field)
? $this->castValue($this->input($field, $default), $casts[$field])
: $this->input($field, $default);
}
/**
* @param array $data
* @return array
*/
protected function castData($data)
{
foreach ($this->casts() as $attribute => $cast) {
if (isset($data[$attribute]) || array_key_exists($attribute, $data)) {
$data[$attribute] = $this->castValue(@$data[$attribute], $cast);
}
}
return $data;
}
/**
* @param mixed $value
* @param array|string|callback $cast
* @return mixed
*/
protected function castValue($value, $cast)
{
if (!is_string($cast) && is_callable($cast)) {
return $cast($value);
} else {
$args = (array) $cast;
$method = array_shift($args);
array_unshift($args, $value);
return call_user_func_array([$this, 'castTo' . Str::camel($method)], $args);
}
}
// The cast methods
/**
* @param $input
* @param string $format
* @return Carbon|null
*/
protected function castToDate($input, $format)
{
return !isset($input) || $input === '' ? null : Carbon::createFromFormat($format, $input);
}
/**
* @param string $input
* @return string|null
*/
protected function castToNull($input)
{
return $input === '' ? null : $input;
}
}