使用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传值,希望对大家有所帮助
如对本文有疑问, 点击进行留言回复!!
【JavaScript笔记(一)】万丈高楼平地起 - 基本概念篇
轻松解决 org.apache.taglibs.standard.tlv.JstlCoreTLV 困惑
vert实践五——Json?Protocol Buffer?FlatBuffers?
[基于tensorflow的人脸检测] 基于神经网络的人脸检测8——验证训练好的神经网络
网友评论