错误消息: 我似乎不明白警告。
"<br />
<b>Warning</b>: Header may not contain more than a single header, new line detected in <b>/Applications/XAMPP/xamppfiles/htdocs/rest_api_myblog/api/post/create.php</b> on line <b>8</b><br />
<br />
<b>Notice</b>: Undefined property: stdClass::$title in <b>/Applications/XAMPP/xamppfiles/htdocs/rest_api_myblog/api/post/create.php</b> on line <b>25</b><br />
<br />
<b>Notice</b>: Undefined property: stdClass::$author in <b>/Applications/XAMPP/xamppfiles/htdocs/rest_api_myblog/api/post/create.php</b> on line <b>27</b><br />
<br />
<b>Notice</b>: Undefined property: stdClass::$category_id in <b>/Applications/XAMPP/xamppfiles/htdocs/rest_api_myblog/api/post/create.php</b> on line <b>28</b><br />
<br />
<b>Warning</b>: strip_tags() expects parameter 1 to be string, object given in <b>/Applications/XAMPP/xamppfiles/htdocs/rest_api_myblog/models/Post.php</b> on line <b>99</b><br />
{"message":"Post Created"}"
Axios
onCreate = event => {
event.preventDefault();
let cat_id = (this.state.cat + 1);
const body = {
'author' :this.state.author,
'title': this.state.title,
'body' : this.state.body,
'category_id':String(cat_id)
};
console.log(body);
axios.post(`http://localhost/rest_api_myblog/api/post/create.php`,{
headers: {
'Accept': 'application/json',
'Content-Type': 'application/x-www-form-urlencoded'
},
body})
.then(res => {console.log(res)
})
.catch(e => console.log(e));
}
当我使用POSTMAN进行发布时,没有一个错误,但是我立即从前端尝试。我收到带有上述警告的“创建帖子”,除了日期和ID(autoIncrement)之外没有创建帖子
PHP:这是我的创建功能
public function create(){
//create query
$query ='INSERT INTO '. $this->table .' SET title = :title,body = :body, author = :author, category_id = :category_id';
//prepare statement
$stmt = $this->conn->prepare($query);
//clean data
$this->title = htmlspecialchars(strip_tags($this->title));
$this->body = htmlspecialchars(strip_tags($this->body));
$this->author = htmlspecialchars(strip_tags($this->author));
$this->category_id = htmlspecialchars(strip_tags($this->category_id));
//bind data
$stmt->bindParam(':title', $this->title);
$stmt->bindParam(':body', $this->body);
$stmt->bindParam(':author', $this->author);
$stmt->bindParam(':category_id', $this->category_id);
// Execute query
if($stmt->execute())
{
return true;
}
// Print error if something goes wrong
printf("Error: %s.\n", $stmt->error);
return false;
}
API:Create.php
<?php
// Headers
header('Access-Control-Allow-Origin: *');
header('Content-Type: application/json');
//add more for creating a post
header('Access-Control-Allow-Methods: POST');
header('Access-Control-Allow-Headers: Access-Control-Allow-headers,
Content-Type, Access-Control-Allow-Methods,
Authorization, X-Requested-with');
include_once '../../config/Database.php';
include_once '../../models/Post.php';
//Instantiate DB & connect
$database = new Database();
$db =$database->connect();
//initiate blog post object
$post = new Post($db);
//we need to get the data was posted
//Get the raw posted data
$data =json_decode(trim(file_get_contents("php://input")));
$post->title = $data->title;
$post->body = $data->body;
$post->author = $data->author;
$post->category_id = $data->category_id;
//create post
if($post->create()) {
echo json_encode(
array('message' => 'Post Created')
);
} else{
echo json_encode(
array('message' => 'Post not created!')
);
}
答案 0 :(得分:1)
这通常在没有正确传递身体参数时很常见。我将建议您是否可以使用fetch代替axios并引入JSON.stringify(body)。但是,我已经为您重写了代码。
onCreate = event => {
event.preventDefault();
let cat_id = (this.state.cat + 1);
const someValue = {
'author' :this.state.author,
'title': this.state.title,
'body' : this.state.body,
'category_id':String(cat_id)
};
fetch(`http://localhost/rest_api_myblog/api/post/create.php`,
{ method: 'POST',
headers: {
'Accept':'application/json',
'Content-Type': 'application/json'
},
body: JSON.stringify(someValue)})
.then(res => {console.log(res)
})
.catch(e => console.log(e));
this.setState({
author: " ",
title: "",
});
}