我刚刚开始使用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回复......任何人都可以帮助我,我错过了什么?
答案 0 :(得分: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周围的引号。
希望这有帮助。
答案 1 :(得分:0)
$.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"
});