我正在尝试使用 api.challonge.com 创建锦标赛
在 node.js 中尝试这样做时,它可以很好地使用以下代码:
request.post("https://api.challonge.com/v1/tournaments.json", {
"json":{
"api_key":API_KEY,
"name":"MyTourney",
}
}, function (error, response, body) {
console.log(body);
});
但是,当尝试在 javascript 中使用 XMLHttpRequest 执行此操作时,我收到错误 422。这是代码:
var fd = new FormData();
fd.append("api_key", API_KEY);
fd.append("name", "MyTourney");
var xhr = new XMLHttpRequest();
xhr.open("POST", "https://api.challonge.com/v1/tournaments.json", true);
xhr.onreadystatechange = function() {
console.log(xhr.responseText);
};
xhr.send(fd);
我不明白为什么在节点请求中请求成功,而在XMLHttpRequest中却没有
这是浏览器控制台的屏幕截图: