我正在为laravel 5.6创建API。 表单数据正在使用OPTIONS方法发送到API。 如何将这些数据输入控制器?
我已经测试过了。
jquery ajax
$.ajax({
url:'domain.com/laravel_app/api/register_user',
type:"options",
dataType:'json',
data:{name:'Joe'},
success:function(r)
{
console.log(r);
}
})
拉威尔(Laravel)路线
Route::match(['post', 'options'], '/register_user/', 'UserController@register_user');
Laravel控制器
public function register_user(Request $request)
{
print_r($request->all());
$arr1['status']='SUCCESS';
return json_encode($arr1);
}
使用“ post”方法可以正常工作,但不能使用“ options”
答案 0 :(得分:0)
这似乎是jQuery忽略the spec on the OPTIONS动词并将您的数据作为请求正文发送的问题。
您应该可以这样做:
$.ajax({
url:'domain.com/laravel_app/api/register_user?'+$.param({name: 'Joe'}),
type:"options",
dataType:'json',
success:function(r)
{
console.log(r);
}
})
但是请记住,根据the spec:
HTTP OPTIONS方法用于描述目标资源的通信选项。
您在此处使用此请求的内容似乎并未用于描述目标资源的通信选项,因此您不应为此使用OPTIONS方法。