【问题标题】:sending post with ajax - bad request issue使用 ajax 发送帖子 - 错误请求问题
【发布时间】:2018-07-27 11:13:53
【问题描述】:

我刚开始使用 API,我需要一点帮助...

我有这个代码:

<!DOCTYPE html>
<html>
<head>
<title></title>
<script 
  src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js">
</script>
</head>
<body>
<script type="text/javascript">
   var json=`{
   "Teamname": "example",
   "Password": "example",
   "members": [{
        "name": "John",
        "surname": "Doe",
    },
    {
        "name": "Kate",
        "surname": "Smith",

    },
    {
        "name": "Brad",
        "surname": "Warden",

    },
    {
        "name": "Antony",
        "surname": "McLeer",

    }
]
}`;
$.ajax({
  type: "POST",
  url: "http://52.233.158.172/change/api/en/account/register",
  data: "json",
  contetType: "application/json"
  });
  console.log(json);

 </script>
 </body>
 </html>

我在控制台中收到了返回错误请求,我多次查看代码,一切都应该正常工作,但显然缺少一些东西

另外,如果我和邮递员一起去,我会得到 200 条 OK 响应...谁能帮我我错过了什么?

【问题讨论】:

  • 您发送json 字符串而不是变量作为数据参数。

标签: jquery json ajax post


【解决方案1】:

当您需要发送变量 json 时,您似乎在您的帖子数据中发送了一个字符串“json”。

如果您将 ajax 请求更新为:

$.ajax({
    method: "POST",
    url: "http://52.233.158.172/change/api/en/account/register",
    data: json,
    contentType: "application/json"
});

请注意在第 4 行删除了 json 周围的引号。

希望这会有所帮助。

【讨论】:

  • 我从控制台得到这个:405(不允许的方法)
  • {错误:[“请求的资源不支持http方法'OPTIONS'。”],结果:null}@karl
  • 基于下面链接的帖子类型只能在 1.9.0 之前的 jQuery 版本中使用。如果您使用的是比该版本更新的版本,则可能需要使用方法。我已更新帖子以显示此更改。 stackoverflow.com/questions/43543174/…
【解决方案2】:
$.ajax({
  type: "POST",
  url: "http://52.233.158.172/change/api/en/account/register",
  data: "json",
  contetType: "application/json"
  });

应该是

$.ajax({
  type: "POST",
  url: "http://52.233.158.172/change/api/en/account/register",
  data: json,
  contentType: "application/json"
  });

【讨论】:

  • contetType 也应该是 contentType ;)
  • 是的错字......但是我从控制台得到这个:405(不允许方法)
  • API 没有公开允许 POST 调用的方法。 Docs
猜你喜欢
  • 2016-12-10
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-04-11
  • 2019-11-15
  • 1970-01-01
相关资源
最近更新 更多