【发布时间】:2017-06-05 21:06:59
【问题描述】:
我正在使用 laravel 5.3 和 angular 2 中的客户端构建 API 服务。当我尝试将登录表单作为 JSON 数据提交时,登录控制器 (Laravel) 中的请求数据数组为空 []。我使用邮递员扩展测试了 API,它可以正常工作并返回 200 状态码。但是当 Angular 发送一个 POST 请求时,我 没有 在我的控制器上接收到任何东西。
Angular 2 Post 调用:
private authUrl = 'http://localhost:35656/api/authenticate';
var payload = JSON.stringify({ "email": email, "password": password });
console.log(payload);
return this.http.post(this.authUrl, payload, {
//headers: headers
}).map((response: Response) => {var status = response.json().status;});
Laravel 路线
header('Access-Control-Allow-Origin:*');
header('Access-Control-Allow-Methods:GET, POST, PUT, DELETE, OPTIONS');
Route::post('authenticate', 'Auth\LoginController@authenticate');
Laravel 控制器
/**
* Return a JWT
*
* @return Response
*/
public function authenticate(Request $request)
{
//$credentials = $request->only('email', 'password');
$credentials = $request->all(); // IS EMPTY
//$credentials = 'testing...';
return response()->json(['status' => true, 'error' => $credentials]);
}
如果我在来自 Angular 的 POST 调用中执行此操作,那么它可以工作。
return this.http.post('http://localhost:35656/api/authenticate?email=xxx@eeeee.com&password=xxxxx', '')
.map((response: Response) => {});
我的 Laravel Sever 是 http://localhost:35656,Angular 2 是 http://localhost:3000。
我做错了什么?
【问题讨论】:
标签: laravel angular laravel-5.3