使用JavaScript获取发布数据的正确方法是什么?

发布于 2025-01-23 07:01:53 字数 1340 浏览 2 评论 0原文

我正在尝试摆脱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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

剩余の解释 2025-01-30 07:01:53

jQuery URL编码数据。您需要调用encodeuricomponent()来执行此操作,然后将其作为data参数的值传递。等效于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/x-www-form-urlencoded'
      },
      body: `data=${encodeURIComponent($data)}`
    })

    .then((response) => {
      console.log(response.text())


    })
});

jQuery URL-encodes the data. You need to call encodeURIComponent() to do that, and then pass it as the value of the data parameter. The equivalent with fetch() is:

$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/x-www-form-urlencoded'
      },
      body: `data=${encodeURIComponent($data)}`
    })

    .then((response) => {
      console.log(response.text())


    })
});

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文