js编程实现将三个json字符串发送至本地php语言为主要架构的网站的数据库中存储起来,要求也要给出网站接收端PHP文件的编程代码,
时间: 2023-05-22 18:01:50 浏览: 70
可以使用XMLHttpRequest对象来实现将三个json字符串发送至本地php语言为主要架构的网站的数据库中存储起来。以下是js代码示例:
var xhr = new XMLHttpRequest();
xhr.open('POST', 'http://localhost/save_json.php', true);
xhr.setRequestHeader('Content-Type', 'application/json');
xhr.onreadystatechange = function() {
if (xhr.readyState === 4 && xhr.status === 200) {
console.log(xhr.responseText);
}
};
var json1 = { "name": "John", "age": 30, "city": "New York" };
var json2 = { "name": "Jane", "age": 25, "city": "Los Angeles" };
var json3 = { "name": "Bob", "age": 40, "city": "Chicago" };
var data = JSON.stringify({ "json1": json1, "json2": json2, "json3": json3 });
xhr.send(data);
接收端PHP文件的编程代码如下:
<?php
header('Content-Type: application/json');
$data = json_decode(file_get_contents('php://input'), true);
$json1 = $data['json1'];
$json2 = $data['json2'];
$json3 = $data['json3'];
// 将json数据存储到数据库中
// ...
echo json_encode(array('success' => true));
?>
相关推荐
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)