来自Angular Js的Json返回值显示

时间:2017-08-09 12:08:59

标签: json angular ionic-framework

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'));
        }

2 个答案:

答案 0 :(得分:0)

在订阅之前映射响应

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));
     }
  });

答案 1 :(得分:0)

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服务类中的post方法

服务

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);
      }
      );
相关问题