虽然我使用Ajax发送请求,但服务器的响应有一个完整的HTML页面而不是预期的简单JOSN对象。
我的发送代码:
<script>
var form = new FormData();
form.append("name", "dasdf");
form.append("email", "admin@admin.com");
form.append("mobile_number", "454545");
form.append("pin", "1234");
form.append("gender", "male");
form.append("allow_users_see_me", "true");
form.append("password", "123456");
form.append("country_code", "QA");
var settings = {
"async": true,
"crossDomain": true,
"url": "http://localhost/register",
"method": "POST",
"headers": {
"X-CSRF-Token": "2oVPtMysQ30esIv6X2LJYghonCAbR4Zt4Op20SEV",
"cache-control": "no-cache",
"postman-token": "b3c3ff77-a12f-7b60-4b53-16fcbcb97469"
},
"processData": false,
"contentType": 'application/json; charset=utf-8',
"data": form
}
$.ajax(settings).done(function (response) {
console.log(response);
});
</script>
有什么想法吗?
修改
public function register(RegisterRequest $request)
{
Log::info('start');
}
修改
我找到了laravel返回异常的地方,它在渲染函数中。
public function render($request, Exception $e)
{
if ($e instanceof HttpResponseException) {
return $e->getResponse();
} elseif ($e instanceof ModelNotFoundException) {
$e = new NotFoundHttpException($e->getMessage(), $e);
} elseif ($e instanceof AuthorizationException) {
$e = new HttpException(403, $e->getMessage());
} elseif ($e instanceof ValidationException && $e->getResponse()) {
return $e->getResponse();
}
if ($this->isHttpException($e)) {
return $this->toIlluminateResponse($this->renderHttpException($e), $e);
} else {
return $this->toIlluminateResponse($this->convertExceptionToResponse($e), $e);
}
}
laravel返回HttpException。
-UPDATE -
这是我的路线
Route::get('test', 'loginController@test');
这是错误
<div class="alert alert-danger">
The name field is required.<br/>
The email field is required.<br/>
The mobile number field is required.<br/>
The pin field is required.<br/>
The gender field is required.<br/>
The password field is required.<br/>
</div>
- 编辑 - 路线是:
路由::帖子(&#39;注册&#39;,&#39; AuthController @ register&#39;);
答案 0 :(得分:0)
在我们交换了评论后, 我认为 您的form
变量要么没有正确设置,PHP也无法读取数据。在发送表单变量之前调整表单变量并看到为什么它没有被正确读取可能是值得的。从理论上说它应该没问题,但是如果你得到The name field is required.
等,我猜错了你不想做的事情。
通常,如果我将所有字段都放在表单中并且使用jQuery,我会这样做:
var form = $("form").serialize(); // or pass the id or other selector
请稍后尝试console.log
变量,看看数据结构与您现在的处理方式有何不同。