当前位置:网站首页>The background receives the post data passed by the fetch
The background receives the post data passed by the fetch
2022-06-23 20:32:00 【It workers】
fetch Official website post Data is transmitted by JSON.stringify() In the form of :
fetch('http://www.tingchunyu.com/test/fetch_test/fetch_getuser_test.php', {
body: JSON.stringify({id:666}),
headers: {
'content-type': 'application/json'
},
method: 'POST',
})
.then(response => {
if (response.ok){// Determine if the request is successful
return response.json()
}
throw new Error(' Request error ')
})
.then(data=>{
console.log(data)
})
.catch(error => {
console.log(error)
})
But like the above, the background always receives variables in the previous way null. The reason for this is that the data submitted to the server is json data , Not traditional formdata. As shown in the figure below :
Therefore, there are two solutions for the background to receive data
Put the above code body Make the following changes to the format of
body: JSON.stringify({id:666}) It is amended as follows body: 'id=666'// If there are multiple parameters, you can use "&" Connect Such as :body: 'id=666&type=0'
But if there are many parameters to be passed in this way, it will be a bit troublesome .
The second method , Keep the above code unchanged . Modify the method of background receiving parameters . With PHP For example :
// take $_POST['id'] Change to the following method
$data = json_decode(file_get_contents('php://input'),true);
$id = $data['id'];// So you can receive ID 了 边栏推荐
- [golang] quick review guide quickreview (III) - Map
- Is it safe for flush to open an account online? Is the Commission high
- SAP实施项目上的内部顾问与外部顾问,相互为难还是相互成就?
- What is the difference between a database and a cloud disk drive? What functions can cloud disk drives achieve?
- 【Golang】怎样优雅的清空切片
- 数字化采购转型解决方案:SaaS采购管理平台推进企业阳光采购
- Application of JDBC in performance test
- Tcp/udp Fundamentals
- Is Huishang futures trading software formal? How to download safely?
- Emmet语法规范
猜你喜欢

80% of people will be wrong about the three counter intuitive questions?

Implementation of microblog system based on SSM

Yaokui tower in Fengjie, Chongqing, after its completion, will be the safety tower for Sichuan river shipping with five local scholars in the company

JS高级程序设计第 4 版:生成器的学习

35岁危机?内卷成程序员代名词了…

小程序开发框架推荐

20省市公布元宇宙路线图

科班出身,结果外包都不要

Rstudio 1.4 software installation package and installation tutorial

Tupu software digital twin intelligent water service, breaking through the development dilemma of sponge City
随机推荐
20省市公布元宇宙路线图
5 月最大的 GameFi 崩溃受害者能否在熊市中生存?| May Monthly Report
Hardware development notes (6): basic process of hardware development, making a USB to RS232 module (5): creating USB package library and associating principle graphic devices
ElastricSearch第二弹之分片原理
想开个户,在股票网上开户安全吗?资金会被骗走吗?
Stochastic process -- Markov chain
国内期货开户怎么开?哪家期货公司开户更安全?
【Golang】快速复习指南QuickReview(二)——切片slice
LeetCode 1079. movable-type printing
[golang] quick review guide quickreview (IV) -- functions
[golang] follow the object pool sync Pool
教你如何用网页开发桌面应用
GL Studio 5 installation and experience
Why is only one value displayed on your data graph?
Can Tencent cloud disk service share data? What are the advantages of cloud disk service?
深入理解和把握数字经济的基本特征
Shell Scripting
手续费佣金低的券商,华泰证券网上开户安全吗
@@Script implementation of ishell automatic deployment
The "open source star picking program" container pulls private images from harbor, which is a necessary skill for cloud native advanced technology