我正在尝试使用 axios formdata 将 js 数组发送到 php,但它一直失败
im trying to send a js array to php using axios formdata but it keep on failing i get [objext object] at console and if im trying to use stringify ""
我正在尝试使用 axios post js 键和值数组到 php,我真的迷路了谢谢你的帮助
这是我要发送的数组:
let arr = [];
arr["adID"] = uuidv4();
arr["price"] = price;
arr["city"] = city;
arr["street"] = street;
arr["rooms"] = rooms;
arr["building_number"] = building_number;
arr["entry"] = entry;
arr["apartment"] = apartment;
arr["user_id"] = 1;
sendDataFromJsToPhp("insertAd", arr);
这是 axios 部分:
sendDataFromJsToPhp(type, parametersArray) {
this.formData.append("data", "insertAd"); //type to create new ad is insertAd
this.formData.append(
"parameters",parametersArray); //type to create new ad is insertAd
axios({
method: "post",
url: this.url,
data: this.formData,
config: { headers: { "Content-Type": "multipart/form-data" } },
}).then(
(response) => {
console.log(response.data);
this.resultFromServer = response.data;
},
(error) => {
console.log(error);
}
);
//this is a work around that will get us array of keys and one of values and will merge them
//to get aray that represent the data we got
return this.resultFromServer;
}
这里是 php:
header("Access-Control-Allow-Origin: *");
header("Access-Control-Allow-Headers: *");
header("Access-Control-Allow-Methods: *");
header('Content-Type: application/json');
$DATA_RAW = file_get_contents("php://input");
$DATA_OBJ = json_decode($DATA_RAW);
$error = "";
if(isset($_POST)&&isset($_POST['data']))
$dataType = $_POST["data"];
if(isset($_POST)&&isset($_POST['parameters']))
$parameters=$_POST['parameters'];
print_r($parameters);
当我处理 $parameters 时出现的错误我得到这些错误:
1)警告: 中未初始化的字符串偏移量 0
2)b>Fatal error: Uncaught TypeError: Cannot access offset of type string on string in
中的字符串
非常感谢
如果您的数组使用字符串键值而不是 int,stringify 会忽略该值。
因此你必须创建一个对象而不是数组。
即;
let arr = {};
arr.adID = uuidv4();
arr.price = price;
arr.city = city;
arr.street = street;
arr.rooms = rooms;
arr.building_number = building_number;
arr.entry = entry;
arr.apartment = apartment;
arr.user_id = 1;
sendDataFromJsToPhp("insertAd", arr);
并在表格中追加数据;
this.formData.append(
"parameters", JSON.stringify(parametersArray)); //type to create new ad is insertAd
我正在尝试使用 axios post js 键和值数组到 php,我真的迷路了谢谢你的帮助
这是我要发送的数组:
let arr = [];
arr["adID"] = uuidv4();
arr["price"] = price;
arr["city"] = city;
arr["street"] = street;
arr["rooms"] = rooms;
arr["building_number"] = building_number;
arr["entry"] = entry;
arr["apartment"] = apartment;
arr["user_id"] = 1;
sendDataFromJsToPhp("insertAd", arr);
这是 axios 部分:
sendDataFromJsToPhp(type, parametersArray) {
this.formData.append("data", "insertAd"); //type to create new ad is insertAd
this.formData.append(
"parameters",parametersArray); //type to create new ad is insertAd
axios({
method: "post",
url: this.url,
data: this.formData,
config: { headers: { "Content-Type": "multipart/form-data" } },
}).then(
(response) => {
console.log(response.data);
this.resultFromServer = response.data;
},
(error) => {
console.log(error);
}
);
//this is a work around that will get us array of keys and one of values and will merge them
//to get aray that represent the data we got
return this.resultFromServer;
}
这里是 php:
header("Access-Control-Allow-Origin: *");
header("Access-Control-Allow-Headers: *");
header("Access-Control-Allow-Methods: *");
header('Content-Type: application/json');
$DATA_RAW = file_get_contents("php://input");
$DATA_OBJ = json_decode($DATA_RAW);
$error = "";
if(isset($_POST)&&isset($_POST['data']))
$dataType = $_POST["data"];
if(isset($_POST)&&isset($_POST['parameters']))
$parameters=$_POST['parameters'];
print_r($parameters);
当我处理 $parameters 时出现的错误我得到这些错误: 1)警告: 中未初始化的字符串偏移量 0 2)b>Fatal error: Uncaught TypeError: Cannot access offset of type string on string in
中的字符串非常感谢
如果您的数组使用字符串键值而不是 int,stringify 会忽略该值。
因此你必须创建一个对象而不是数组。
即;
let arr = {};
arr.adID = uuidv4();
arr.price = price;
arr.city = city;
arr.street = street;
arr.rooms = rooms;
arr.building_number = building_number;
arr.entry = entry;
arr.apartment = apartment;
arr.user_id = 1;
sendDataFromJsToPhp("insertAd", arr);
并在表格中追加数据;
this.formData.append(
"parameters", JSON.stringify(parametersArray)); //type to create new ad is insertAd