小编Vik*_*ski的帖子

Wordpress 在使用 fetch API 进行 ajax 调用时返回 400 Bad Request

我正在使用 fetch API 语法对 admin-ajax.php 进行 Ajax 调用。下面是调用后端脚本的代码:

fetch(ajax_obj.ajaxurl, {
    method: "POST",
    mode: "cors",
    cache: "no-cache",
    credentials: "same-origin",
    body: JSON.stringify(this.data),
    headers: {
      "Content-Type": "application/json"
    }
  })
Run Code Online (Sandbox Code Playgroud)

并收到 400 Bad Request 响应。

谁能告诉我这个要求哪里不对?

在chrome中检查网络开发工具时,我可以看到发送的正文没问题,url也没问题……据我所知,4xx状态代码是客户端错误,所以我甚至不看服务器端代码...如果我错了,请给我反馈...

实际上,我有这样的 jQuery ajax 调用:

this.data = {
              'action': 'ajax_product_query',
              'locations': this.locations,
              'type': this.category != '' ? this.category : [],
              'all-locations': this.filters['locationFilter'].all.checked,
              'page': ajax_obj.current_page
            };

          $.ajax({
            url: ajax_obj.ajaxurl,
            method: 'post',
            dataType: 'json',
            data: this.data,
            beforeSend: function(xhr) {
              button.innerHTML = 'Loading...';
            },
            success: (data) => {  ...
Run Code Online (Sandbox Code Playgroud)

...它就像一个魅力...

然后,愿意删除 …

ajax wordpress fetch-api

5
推荐指数
1
解决办法
2544
查看次数

标签 统计

ajax ×1

fetch-api ×1

wordpress ×1