fetch() 发布请求操作

时间:2021-01-09 15:36:31

标签: ajax wordpress fetch

这个ajax有一个action,可以用来运行函数并在回调函数中访问数据值作为$_POST['mydata']

 $.ajax({
      url: qrAjax.ajax_url,
      type: 'POST',
      context: this,
      data: {
        action: 'qrcode_post_create',
        nonce: qrAjax.nonce,
        mydata : datas,
      },
      success: function(response) {
        alert("Success");
        $(".qrresponse").append(response);
      },
    });

有没有办法用 fetch() 做类似的事情?

 fetch('https://mywebsite.online/wp-json/wp/v2/qrcode', {
      method: 'POST',
      credentials: 'same-origin',
      headers: new Headers({
        'Content-Type': 'application/json;charset=UTF-8',
        'X-WP-Nonce' : qrAjax.nonce
      }),
      body: JSON.stringify(OurPostData),
    }).then(response => {
      console.log(response);
      return response.json();
    }).then(data => console.log(data));

0 个答案:

没有答案