来自 Angular Js 的 Json 返回值显示



Angular js script


this.http.post(url, body, options)
.subscribe((data) =>
{
if(data.status === 200)
{
this.hideForm   = true;
this.sendNotification(console.log(data.meesage));
}
});

PHP代码

如何通过this.sendNotification((获取json_encode成功消息

$sql  = "INSERT INTO eastcost_school_room(school_room_name, created) VALUES(:name, Now())";
$stmt    = $pdo->prepare($sql);
$stmt->bindParam(':name', $name, PDO::PARAM_STR);
$stmt->execute();
echo json_encode(array('message' => 'Congratulations the record ' . $name . ' was added to the database'));
}

在订阅之前映射响应

this.http.post(url, body, options)
.map((res:Response) => res.json());
.subscribe((data) =>
{
if(data.status === 200)
{
this.hideForm   = true;
this.sendNotification(console.log(data.meesage));
}
});

Php 代码

$sql  = "INSERT INTO eastcost_school_room(school_room_name, created) VALUES(:name, Now())";
$stmt    = $pdo->prepare($sql);
$stmt->bindParam(':name', $name, PDO::PARAM_STR);
$stmt->execute();
$response["success"] = 1;  

$response["message"] = 'Congratulations the record ' . $name . ' was added to the database';  
// echoing JSON response  
echo json_encode($response);  

API 服务类中的发布方法

服务

insert(parameters): Observable<any> {
return this.http.post('url', body, {
headers: headers
})
.map((res: any) => res.json())
}

并订阅您班级的响应

this.service.insert(parameters)
.subscribe(
response => {
console.log(response);
if (response.success == "1") {
console.log("Successfull login");
}
else {
alert(" Invalid user");

}

},
error => {
alert(error);
}
);

最新更新