我目前正在开发一个Angular 2前端和Slim 3 PHP后端。我尝试发帖请求时收到以下错误。
Angular Service
create(user: any) {
console.log(user);
var headers = new Headers();
headers.append('Accept', 'application/x-www-form-urlencoded');
headers.append('Content-Type', 'application/x-www-form-urlencoded');
let options = new RequestOptions({ headers: headers });
return this.http.post('http://localhost:8080/public/auth/signup', user , options)
.map((response: Response) =>response.json());
}
我使用上述服务的角度组件
register(event, full_name, email, password) {
event.preventDefault();
this.userService.create(JSON.stringify({"full_name": full_name,"email": email,"password": password}))
.subscribe(
data => this.postData = data,
error => alert(error),
() => console.log("Finished")
);
}
最后PHP部分
public function postSignUp($request,$response)
{
$validation = $this->validator->validate($request, [
'full_name' => v::notEmpty()->alpha(),
'email' => v::noWhitespace()->notEmpty()->email()->EmailAvailable(),
'password' => v::noWhitespace()->notEmpty(),
]);
if($validation->failed()) {
return $response;
}
$new_user = $this->db->insert("users", [
"full_name" => $request->getParam('full_name'),
"email" => $request->getParam('email'),
"password" => password_hash($request->getParam('password'), PASSWORD_DEFAULT),
]);
$this->flash->addMessage('info', 'You have been signed up!');
$auth = $this->auth->attempt(
$request->getParam('email'),
$request->getParam('password')
);
$registered_user = [
"id" => $new_user,
"full_name" => $request->getParam('full_name'),
"email" => $request->getParam('email')
];
echo json_encode($registered_user);
return $response;
}
关于什么似乎是问题的任何想法?谢谢!
答案 0 :(得分:0)
如果您要发送表单,则需要使用URLSearchParams。
这应该有效:
组件:
register(event, full_name, email, password) {
event.preventDefault();
this.userService.create(full_name, email, password)
.subscribe(
data => this.postData = data,
error => alert(error),
() => console.log("Finished")
);
}
服务:
create(full_name, email, password) {
let body = new URLSearchParams()
body.set('full_name', full_name);
body.set('email', email);
body.set('password', password);
var headers = new Headers();
headers.append('Accept', 'application/x-www-form-urlencoded');
headers.append('Content-Type', 'application/x-www-form-urlencoded');
let options = new RequestOptions({ headers: headers });
return this.http.post('http://localhost:8080/public/auth/signup', body.toString(), options)
.map((response: Response) =>response.json());
}