[英]PHP CodeIgniter API not receiving JSON data
我正在使用 PHP CodeIgniter 與 MYSQL 數據庫和 AngularJS 前端配對創建一個 REST API。 這就是我的數據庫表的樣子: https ://i.stack.imgur.com/piMZS.png
什么有效
account_name : "Test User 2"
display_name : "Test 2"
phone_no : 2832532
email : james@gmail.com
Array
(
[account_name] => Test User 2
[display_name] => Test 2
[phone_no] => 2832532
[email] => james@gmail.com
)
什么不起作用
onSubmit(){
let headers = new HttpHeaders();
headers.append("Content Type", "application/json");
headers.append("Accept", 'application/json');
let postData = {
account_name : "Test User 2",
display_name : "Test 2",
phone_no : "2832532",
email : "james@gmail.com"
}
this.http.post("http://localhost:8080/api/user-info/create-user", JSON.stringify(postData), {headers: headers})
.subscribe(data => {
console.log(data);
}, error => {
console.log(error);
console.log(JSON.stringify(postData))
})
}
}
{"account_name":"Test User 2",
"display_name":"Test 2",
"phone_no":"2832532",
"email":"james@gmail.com"}
code: 500
message: "Column 'account_name' cannot be null"
title: "mysqli_sql_exception"
Array
(
[account_name] =>
[display_name] =>
[phone_no] =>
[email] =>
)
對此的任何幫助將不勝感激。 謝謝
從角度您將數據發布為 JSON。 我的猜測是,使用 POSTMAN,您可以將數據作為表單數據發送。
您可以嘗試以下方法:
onSubmit(){
var formData: any = new FormData();
formData.append("account_name", "Test User 2");
formData.append("display_name", "Test 2");
formData.append("phone_no", "2832532");
formData.append("email", "james@gmail.com");
this.http.post("http://localhost:8080/api/user-info/create-user", formData)
.subscribe(data => {
console.log(data);
}, error => {
console.log(error);
console.log(formData)
})
}
或者更改您的 PHP 代碼以接受 JSON。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.