通过Ionic的RestApi将数据发布到MySql数据库

时间:2018-04-17 23:01:23

标签: php mysql json rest ionic-framework

所以我在我的离子应用程序中有一个表单,用户输入一些信息并在他们点击提交按钮时将其发布到数据库。我将对象字符串化为一个json对象,然后通过我的rest api将它发送到我的数据库。我面临的问题是,当我的json字符串到达​​我的休息api时,它不知道如何正确解码它并抛出多个错误,例如意外的令牌<解析期间的http和http失败。我检查了其余的api输出,看起来它有一个问题试图获得该属性。我已经通过chrome海报测试了它,似乎我的问题是当ionic创建json字符串时,它不会在参数名称周围添加双引号。因此,不是我的json看起来像关键本身的引用。

{"id": null,"title":"Testing Rest","message":"Fire reported at (site) (where) at (time) \u2013 evacuate to parking Lot evacuation points",
"severityLevel":"1","description":"There is a fire in the mens bathroom next to Chrome nightclub","scheduleAt":"2017-12-12 05:50:00",
"updatedAt":"2017-12-12 17:50","latitude":"33.28130","longitude":"-111.97324"}

看起来没有报价。

{id: null,title:"Testing Rest",message:"Fire reported at (site) (where) at (time) \u2013 evacuate to parking Lot evacuation points",
severityLevel:"1",description:"There is a fire in the mens bathroom next to Chrome nightclub",scheduleAt:"2017-12-12 05:50:00",
updatedAt:"2017-12-12 17:50",latitude:"33.28130",longitude:"-111.97324"}

因此,当我通过Chrome海报进行测试时,api会接受带引号的第一个,但它不接受没有它的那个。我认为它必须是我的api中不接受它的东西,但我不知道如何解决这个问题。

My Rest Api - 创建功能

 // create alert
    function create(){

        // query to insert record
        $query = "INSERT INTO
                " . $this->table_name . "
                SET
                    title=:title, message=:message, severityLevel=:severityLevel, description=:description, scheduleAt=:scheduleAt, 
                    updatedAt=:updatedAt, latitude=:latitude, longitude=:longitude";

        // prepare query
        $stmt = $this->conn->prepare($query);

        // sanitize
        $this->title=htmlspecialchars(strip_tags($this->title));
        $this->message=htmlspecialchars(strip_tags($this->message));
        $this->severityLevel=htmlspecialchars(strip_tags($this->severityLevel));
        $this->description=htmlspecialchars(strip_tags($this->description));
        $this->scheduleAt=$this->scheduleAt;
        $this->updatedAt=$this->updatedAt;
        $this->latitude=htmlspecialchars(strip_tags($this->latitude));
        $this->longitude=htmlspecialchars(strip_tags($this->longitude));

        // bind values
        $stmt->bindParam(":title", $this->title);
        $stmt->bindParam(":message", $this->message);
        $stmt->bindParam(":severityLevel", $this->severityLevel);
        $stmt->bindParam(":description", $this->description);
        $stmt->bindParam(":scheduleAt", $this->scheduleAt);
        $stmt->bindParam(":updatedAt", $this->updatedAt);
        $stmt->bindParam(":latitude", $this->latitude);
        $stmt->bindParam(":longitude", $this->longitude);

        // execute query
        if($stmt->execute()){
            return true;
        }

        return false;

    }

休息Api create.php

<?php
// required headers
header("Access-Control-Allow-Origin: *");
header("Content-Type: application/json; charset=UTF-8");
header("Access-Control-Allow-Methods: POST");
header("Access-Control-Max-Age: 3600");
header("Access-Control-Allow-Headers: Content-Type, Access-Control-Allow-Headers, Authorization, X-Requested-With");

// get database connection
include_once '../config/database.php';

// instantiate alert object
include_once '../objects/alert.php';

$database = new Database();
$db = $database->getConnection();

$alert = new Alert($db);

// get posted data
$data = json_decode(file_get_contents("php://input"));

// set alert property values
$alert->title = $data->title;
$alert->message = $data->message;
$alert->severityLevel = $data->severityLevel;
$alert->description = $data->description;
$alert->scheduleAt = $data->scheduleAt;
$alert->updatedAt = $data->updatedAt;
$alert->latitude = $data->latitude;
$alert->longitude = $data->longitude;


// create the alert
if($alert->create()){
    echo '{';
        echo '"message": "Alert was created."';
    echo '}';
}

// if unable to create the alert, tell the user
else{
    echo '{';
        echo '"message": "Unable to create alert."';
    echo '}';
}
?>

在Ionic中,我正在按照这个字符串化我的对象

JSON.stringify(this.alert);

然后我将this.alert传递给我的提供者发送给我的api。

 newAlert(data) {
    return new Promise(resolve => {
      this.http.get(this.apiUrl+'/alerts/create.php?s=' + data)
      .subscribe(data => {
        resolve(data);
      }, err => {
        console.log(err);
      });
    });
  }

0 个答案:

没有答案