所以我在我的爱奥尼亚应用程序中有一个表单,当用户点击提交按钮时,它会输入一些信息并发布到数据库中。我将对象字符串化为json对象,然后通过restapi将其发送到数据库。我面临的问题是,当我的json字符串到达restapi时,它不知道如何正确地对其进行解码,并在解析过程中抛出多个错误,例如意外的token<和http失败。我检查了restapi输出,似乎在尝试获取属性时遇到了问题。我已经通过chromeposter对它进行了测试,我的问题似乎是,当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中的某个东西不接受它,但我不知道如何修复它。
我的RESTAPI-创建函数
// 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;
}
rest api创建.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 '}';
}
?>
在爱奥尼亚,我像这样把我的物体串起来
JSON.stringify(this.alert);
然后我将这个.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);
});
});
}
暂无答案!
目前还没有任何答案,快来回答吧!