fetch使用及如何接收JS传值
使用fetch基本方式:

站在用户的角度思考问题,与客户深入沟通,找到咸阳网站设计与咸阳网站推广的解决方案,凭借多年的经验,让设计与互联网技术结合,创造个性化、用户体验好的作品,建站类型包括:成都做网站、网站设计、企业官网、英文网站、手机端网站、网站推广、申请域名、网站空间、企业邮箱。业务覆盖咸阳地区。
fetch('https://mywebsite.com/endpoint/', {
method: 'POST',
headers: { 'Accept': 'application/json', 'Content-Type': 'application/json'},
body: JSON.stringify({ username: 'username', password: 'password'})
}).then(function(res){
console.log(res)
})方式一:增加headers 定义
在headers头部定义如下:
headers: {'Content-Type': 'application/x-www-form-urlencoded'},同时body传值使用如下方式:
body:'username='+uname+'&password='+password
在php中使用如下接收
input('username')方式二:改变php中接受方式
接受方式如下:
$arr = file_get_contents("php://input");返回字符串对象,使用值需要做如下处理:
$result=array();
foreach (explode('&', $arr) as $t){
list($a,$b)=explode('=', $t);
$result[$a]=$b;
}此时便可以如下接收传值:
$result['username']
总结
以上所述是小编给大家介绍的fetch 使用及如何接收JS传值,希望对大家有所帮助,如果大家有任何疑问欢迎给我留言,小编会及时回复大家的!
当前题目:fetch使用及如何接收JS传值
本文路径:http://www.jxjierui.cn/article/iedoeh.html


咨询
建站咨询
