使用 Javascript 处理 XML 响应
Handling XML response with Javascript
我正在向 API 请求一些信息。我需要发送一些信息以获取所需的信息。响应采用 XML 格式。当我发出请求时,出现以下错误
Uncaught (in promise) TypeError: Failed to execute 'fetch' on 'Window': Request with GET/HEAD method cannot have body.
如果 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=...
我正在向 API 请求一些信息。我需要发送一些信息以获取所需的信息。响应采用 XML 格式。当我发出请求时,出现以下错误
Uncaught (in promise) TypeError: Failed to execute 'fetch' on 'Window': Request with GET/HEAD method cannot have body.
如果 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=...