我正在向 API 请求一些信息。我需要发送一些信息才能取回所需的信息。响应采用 XML 格式。当我提出请求时,出现以下错误
未捕获( promise 中)类型错误:无法在“窗口”上执行“获取”:使用 GET/HEAD 方法的请求不能有正文。
如果 GET 请求没有正文,我该如何发送所需的信息?基本上我如何让它发挥作用?
这是我的代码。
getResponse = () => {
const url = 'http://api.pixelz.com/REST.svc/Templates/';
// The data we are going to send in our request
let data = JSON.stringify({
"contactEmail": "myemail@gmail.com",
"contactAPIkey": "MY-API-KEY"
})
// The parameters we are gonna pass to the fetch function
let fetchData = {
method: 'GET',
body: data,
headers: new Headers()
}
fetch(url, fetchData)
.then(function(response) {
// Handle response you get from the server
response.text()
.then(data => console.log(data))
});
}
最佳答案
事实上,GET 请求不能有正文,这意味着您在获取时不会发送数据。这里可能发生两件事。
- 该特定端点应该使用另一种方法,例如
POST
- 您要发送的数据实际上需要作为查询字符串参数传递
http://api.pixelz.com/REST.svc/Templates/?contactAPIkey=....&contactEmail=...
关于javascript - 使用 Javascript 处理 XML 响应,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43124354/