微信小程序代码:
wx.request({
url: 'https://www.jiayou.art/ajax/TestAjax.php',
data:{
'name':"lishuang"
},
header:{
'content-type': 'application/x-www-form-urlencoded'
},
method:'post',
success(e){
console.log(e)
}
})
//微信小程序默认值是'content-type': 'application/json'
//你需要改成'content-type': 'application/x-www-form-urlencoded'
PHP代码:
<?php
$data = $_POST["name"];
echo $data;
?>
官网文档:
请求的 header 中不能设置 Referer,content-type
默认为application/json
。
data参数说明:
最终发送给服务器的数据是 String 类型,如果传入的 data 不是 String 类型,会被转换成 String 。转换规则如下:
- 对于
GET
方法的数据,会将数据转换成 query string(encodeURIComponent(k)=encodeURIComponent(v)&encodeURIComponent(k)=encodeURIComponent(v)...
) - 对于
POST
方法且header['content-type']
为application/json
的数据,会对数据进行 JSON 序列化 - 对于
POST
方法且header['content-type']
为application/x-www-form-urlencoded
的数据,会将数据转换成 query string(encodeURIComponent(k)=encodeURIComponent(v)&encodeURIComponent(k)=encodeURIComponent(v)...)