使用JavaScript获取发布数据的正确方法是什么?
我正在尝试摆脱jQuery,并开始使用承诺和箭头功能。尽我所能,我似乎无法使用JavaScript获取将数据传递给PHP。
$btnarrmem.addEventListener('click',()=>{
let $url = "getMember.php";
let $data = {vid: '527571'}
$data = JSON.stringify($data);
console.log ($data);
fetch ($url, {
method:'POST',
headers :{
'Accept': 'application/json',
'Content-Type': 'application/json;charset=utf-8'
},
body:$data
})
.then((response)=>{
console.log (response.text())
})
结果始终是
Promise {<pending>}
[[Prototype]]: Promise
[[PromiseState]]: "fulfilled"
[[PromiseResult]]: *"Notice</b>: Undefined index: data in /mnt/development/websites/arrows/getMember.php< on line <b>7<*emphasized text**
对我来说,PHP没有获得数据。
这是使用jQuery编写的相同功能,就像我想要的那样,
let $data = {vid: '527571'}
$data = JSON.stringify($data);
console.log($data);
$('button.qrymem').click(function () {
let $data = {vid: '527571'}
$data = JSON.stringify($data);
console.log ($data);
$.post('getMember.php', {data: $data}, function ($result) {
console.log($result);
$divquery.text($result);
})
})
它可以欣赏任何建议,以使我朝着正确的方向前进。
I am trying to get away from jQuery and start using promises and arrow functions. Try as I might, I can't seem to pass data to PHP using javascript fetch.
$btnarrmem.addEventListener('click',()=>{
let $url = "getMember.php";
let $data = {vid: '527571'}
$data = JSON.stringify($data);
console.log ($data);
fetch ($url, {
method:'POST',
headers :{
'Accept': 'application/json',
'Content-Type': 'application/json;charset=utf-8'
},
body:$data
})
.then((response)=>{
console.log (response.text())
})
The result is always
Promise {<pending>}
[[Prototype]]: Promise
[[PromiseState]]: "fulfilled"
[[PromiseResult]]: *"Notice</b>: Undefined index: data in /mnt/development/websites/arrows/getMember.php< on line <b>7<*emphasized text**
Which means to me that PHP is not getting data.
Here is the same function written using jQuery and it works just as I want it to
let $data = {vid: '527571'}
$data = JSON.stringify($data);
console.log($data);
$('button.qrymem').click(function () {
let $data = {vid: '527571'}
$data = JSON.stringify($data);
console.log ($data);
$.post('getMember.php', {data: $data}, function ($result) {
console.log($result);
$divquery.text($result);
})
})
I'd appreciate any suggestions to get me moving in the proper direction.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
jQuery URL编码数据。您需要调用
encodeuricomponent()
来执行此操作,然后将其作为data
参数的值传递。等效于fetch()
是:jQuery URL-encodes the data. You need to call
encodeURIComponent()
to do that, and then pass it as the value of thedata
parameter. The equivalent withfetch()
is: