Angular 7 POST方法到PHP服务器不工作
我正在使用Angular 7 POST方法到PHP服务器不工作,php,angular,rest,Php,Angular,Rest,我正在使用HttpClient将数据从Angular 7发布到PHP服务器。 反应式表单从用户处获取必要的数据,然后将其发布到具有以下内容的url: 管理组件.ts: constructor(private postMedicineService: MedicineService) { } ngOnInit() { this.medicineInfo = new FormGroup({ name: new FormControl(null, Validators.requ
HttpClient
将数据从Angular 7发布到PHP服务器。
反应式表单从用户处获取必要的数据,然后将其发布到具有以下内容的url:
管理组件.ts
:
constructor(private postMedicineService: MedicineService) { }
ngOnInit() {
this.medicineInfo = new FormGroup({
name: new FormControl(null, Validators.required),
description: new FormControl(null, Validators.required),
category: new FormControl(null, Validators.required),
image: new FormControl(null, Validators.required),
price: new FormControl(null, Validators.required)
});
}
submitMedicineInfo() {
this.postMedicineService.postMedicine(this.medicineInfo.value)
.subscribe(
(response) => console.log(response),
(error) => console.log(error)
);
}
const postUrl = 'http://localhost/petrapharm-backend/create.php';
@Injectable()
export class MedicineService {
constructor(private httpClient: HttpClient) { }
postMedicine(medicine: Medicine) {
console.log(medicine);
return this.httpClient.post(postUrl, medicine);
}
}
export interface Medicine {
name: String;
description: String;
category: String;
image: File;
price: number;
}
medicine.service.ts
:
constructor(private postMedicineService: MedicineService) { }
ngOnInit() {
this.medicineInfo = new FormGroup({
name: new FormControl(null, Validators.required),
description: new FormControl(null, Validators.required),
category: new FormControl(null, Validators.required),
image: new FormControl(null, Validators.required),
price: new FormControl(null, Validators.required)
});
}
submitMedicineInfo() {
this.postMedicineService.postMedicine(this.medicineInfo.value)
.subscribe(
(response) => console.log(response),
(error) => console.log(error)
);
}
const postUrl = 'http://localhost/petrapharm-backend/create.php';
@Injectable()
export class MedicineService {
constructor(private httpClient: HttpClient) { }
postMedicine(medicine: Medicine) {
console.log(medicine);
return this.httpClient.post(postUrl, medicine);
}
}
export interface Medicine {
name: String;
description: String;
category: String;
image: File;
price: number;
}
medicine object.service.ts
:
constructor(private postMedicineService: MedicineService) { }
ngOnInit() {
this.medicineInfo = new FormGroup({
name: new FormControl(null, Validators.required),
description: new FormControl(null, Validators.required),
category: new FormControl(null, Validators.required),
image: new FormControl(null, Validators.required),
price: new FormControl(null, Validators.required)
});
}
submitMedicineInfo() {
this.postMedicineService.postMedicine(this.medicineInfo.value)
.subscribe(
(response) => console.log(response),
(error) => console.log(error)
);
}
const postUrl = 'http://localhost/petrapharm-backend/create.php';
@Injectable()
export class MedicineService {
constructor(private httpClient: HttpClient) { }
postMedicine(medicine: Medicine) {
console.log(medicine);
return this.httpClient.post(postUrl, medicine);
}
}
export interface Medicine {
name: String;
description: String;
category: String;
image: File;
price: number;
}
最后,我有一个PHP服务器,它从Angular 7应用程序接收POST请求,并具有以下内容:
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 './database.php';
// instantiate product object
include_once './medicine.php';
$database = new Database();
$db = $database->getConnection();
$product = new Medicine($db);
// get posted data
$data = json_decode(file_get_contents("php://input"));
// make sure data is not empty
if(
!empty($data->name) &&
!empty($data->description) &&
!empty($data->category) &&
!empty($data->image) &&
!empty($data->price)
){
// set product property values
$product->name = $data->name;
$product->description = $data->description;
$product->category = $data->category;
$product->image = $data->image;
$product->price = $data->price;
// create the product
if($product->create()){
// set response code - 201 created
http_response_code(201);
// tell the user
echo json_encode(array("message" => "Product was created."));
}
// if unable to create the product, tell the user
else{
// set response code - 503 service unavailable
http_response_code(503);
// tell the user
echo json_encode(array("message" => "Unable to create product."));
}
}
// tell the user data is incomplete
else{
// set response code - 400 bad request
http_response_code(400);
// tell the user
echo json_encode(array("message" => "Unable to create product. Data is incomplete."));
}
?>
<?php
class Medicine{
// database connection and table name
private $conn;
private $table_name = "medicine";
// object properties
public $id;
public $name;
public $description;
public $category;
public $image;
public $price;
// constructor with $db as database connection
public function __construct($db){
$this->conn = $db;
}
// create product
function create(){
// query to insert record
$query = "INSERT INTO
" . $this->table_name . "
SET
name=:name, description=:description, category=:category, image=:image, price=:price";
// prepare query
$stmt = $this->conn->prepare($query);
// sanitize
$this->name=htmlspecialchars(strip_tags($this->name));
$this->description=htmlspecialchars(strip_tags($this->description));
$this->category=htmlspecialchars(strip_tags($this->category));
$this->image=htmlspecialchars(strip_tags($this->image));
$this->price=htmlspecialchars(strip_tags($this->price));
// bind values
$stmt->bindParam(":name", $this->name);
$stmt->bindParam(":description", $this->description);
$stmt->bindParam(":category_id", $this->category);
$stmt->bindParam(":created", $this->image);
$stmt->bindParam(":price", $this->price);
// execute query
if($stmt->execute()){
return true;
}
return false;
}
}
它在chrome中给我的错误是:
zone.js:3243选项400(错误请求)
CORS策略已阻止从源“”访问“”处的XMLHttpRequest:对飞行前请求的响应未通过访问控制检查:它没有HTTP ok状态
我还试着在失眠和邮递员中运行POST请求,但他们返回以下休息
{
"message": "Unable to create product. Data is incomplete."
}
这是由于浏览器的CORS策略。浏览器可以将不同的端口解释为不同的源,因为angular代码在4200上提供,而localhost可能在8100上提供,所以您将在浏览器中看到此错误 对于解决方法,您可以尝试在Chrome浏览器中禁用CORS,如果您在Mac上,这将从终端运行:
open -a Google\ Chrome --args --disable-web-security --user-data-dir=""
对于windows,此答案涵盖了一个解决方案:错误答案IMO,因为它没有向op解释CORS,并提供了一个解决方案,但没有说明这是一个解决方案。@EliasSoares假设已经了解了很多内容,但它已经更新。感谢您的回答,但它仍然不起作用,邮递员请求也不起作用,而且不需要帖子。你是不是先完全关闭了浏览器?搜索XSS和CORS。您的api服务器必须使用一些特殊的头来响应选项调用,这些头告诉浏览器它是否可以执行该请求。在大多数浏览器上,这是一种安全模式,您必须遵循它。此外,阅读您的代码,我建议您使用FormBuilder类,它将使您的表单创建更简单、更干净,而无需实例化这么多表单控件和表单组。感谢您的回答,但它仍然无法工作,邮递员请求也无法工作,而且不需要邮寄。谢谢你的建议,我将研究FormBuilder类:)