【问题标题】:400 bad request despite correct request handling400 错误请求,尽管请求处理正确
【发布时间】:2019-05-05 06:08:40
【问题描述】:

我无法让 AJAX 请求正常工作。从这个较早的线程看来,请求处理程序和注册处理程序的钩子不是问题:Wordpress: Ajax request logs 400 Bad Request to console

框架(wordpress)确实每分钟左右发送一个带有“heartbeat”k/v 对的 ajax 请求(到同一个目标)。我从下面的核心复制了我的错误请求和其中一个好的请求。突出的是我的 text/html 的响应标头。这会导致不好的 REQUEST 吗?如果不是还有什么可能导致它?


请求标头

// my bad request. Error code 400 "BAD REQUEST"
Request URL:http://localhost/wptest2/wp-admin/admin-ajax.php
Request method:POST
Remote address:127.0.0.1:80
Status code: 400
Version:HTTP/1.1

Response headers (177 B)    
Raw headers
Connection  close
Content-Length  165
Content-Type    text/html; charset=UTF-8
Date    Sun, 05 May 2019 04:58:48 GMT
Server  Apache/2.4.29 (Ubuntu)

Request headers (1.030 KB)  
Raw headers
Accept    application/json, text/javascript, */*; q=0.01
Accept-Encoding gzip, deflate
Accept-Language  en-US,en;q=0.5
Connection  keep-alive
Content-Length  30
Content-Type    application/json
Cookie    wordpress_69cb7e12fdbb27dfb560…2-1ebb-4ab4-ba67-14f7be9e4c98
Host    localhost
Referer http://localhost/wptest2/wp-ad…-general.php?page=fvc-settings
User-Agent  Mozilla/5.0 (X11; Ubuntu; Linu…) Gecko/20100101 Firefox/66.0
X-Requested-With    XMLHttpRequest


// A working request done automatically every minute or two by wordpress core
Request URL:http://localhost/wptest2/wp-admin/admin-ajax.php
Request method:POST
Remote address:127.0.0.1:80
Status code: 200
Version:HTTP/1.1

Response headers (605 B)    
Raw headers
Cache-Control     no-cache, must-revalidate, max-age=0
Connection  Keep-Alive
Content-Length   423
Content-Type     application/json; charset=UTF-8
Date    Sun, 05 May 2019 04:59:46 GMT
Expires Wed, 11 Jan 1984 05:00:00 GMT
Keep-Alive    timeout=5, max=100
Referrer-Policy strict-origin-when-cross-origin
Server  Apache/2.4.29 (Ubuntu)
X-Content-Type-Options  nosniff
X-Frame-Options SAMEORIGIN
X-QM-overview-memory    7,444 kB
X-QM-overview-memory_usage  5.7% of 131,072 kB limit
X-QM-overview-time_taken    0.5868
X-QM-overview-time_usage    2.0% of 30s limit
X-Robots-Tag    noindex

Request headers (1.062 KB)  
Raw headers
Accept    application/json, text/javascript, */*; q=0.01
Accept-Encoding gzip, deflate
Accept-Language en-US,en;q=0.5
Connection  keep-alive
Content-Length  99
Content-Type    application/x-www-form-urlencoded; charset=UTF-8
Cookie    wordpress_69cb7e12fdbb27dfb560…2-1ebb-4ab4-ba67-14f7be9e4c98
Host    localhost
Referer http://localhost/wptest2/wp-ad…-general.php?page=fvc-settings
User-Agent  Mozilla/5.0 (X11; Ubuntu; Linu…) Gecko/20100101 Firefox/66.0
X-Requested-With    XMLHttpRequest

Ajax 调用

当然,我一次只测试一个。

jQuery(document).ready(function() {
      jQuery('#previousButton').click(function() {
        jQuery.ajax({
          type: "POST",
          url: ajaxurl,
          headers : {
            'Content-Type' : 'application/json'
          },
          dataType: "json",
          action: "selectTargetsWithOffset",
          data: {
            action: "selectTargetsWithOffset",
          },
          success: function() {
            console.log(response, `=====response from $.ajax}=====`);
          }
        });

      });

【问题讨论】:

  • 您是说数据类型是 json 但您传递的数据不是有效的 json。
  • @CarlosAlvesJorge 的评论似乎有效。试试这个:var data = { action: "selectTargetsWithOffset" }; 然后在ajax 调用中使用这个:data: JSON.stringify(data)
  • 可以参考这个问题:stackoverflow.com/questions/12693947/…

标签: jquery ajax


【解决方案1】:

在第一次查看您的问题时,我认为您的问题可能是格式问题 - 您的大部分文件看起来“已被注释掉”。如果评论是故意的 [而不是 SO 帖子中的错误],我建议您首先制作文件的备份副本,然后删除文件中任何不必要的部分。

那么:

  1. 清除您的 cookie;无效/过期的会话 cookie 有时会导致 400 错误。

  2. 检查您访问的网址是否正常(没有拼写错误等)

  3. 检查您提交的文件的大小是否足够小以被接受。太大的文件可能会被服务器拒绝并返回 400 错误

一定要检查您的文件是否存在格式错误和特殊字符。 (为什么在接受编码之前是;q=0.01?)

希望这会有所帮助。

附:你可能会发现这个article 很有帮助

【讨论】:

    猜你喜欢
    • 2014-09-22
    • 1970-01-01
    • 2020-04-20
    • 2021-12-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-11-15
    • 1970-01-01
    相关资源
    最近更新 更多