简体   繁体   English

使用Java处理XML回应

[英]Handling XML response with Javascript

I'm making a request to an API for some information. 我正在向API请求某些信息。 I need to send some information in order to get the required information back. 我需要发送一些信息以获取所需的信息。 The response is in XML format. 响应为XML格式。 When I make the request I get the following error 当我发出请求时,出现以下错误

Uncaught (in promise) TypeError: Failed to execute 'fetch' on 'Window': Request with GET/HEAD method cannot have body.

If a GET request can't have a body how do I send the required information? 如果GET请求没有正文,该如何发送所需信息? Basically how do I get this to work? 基本上我该如何工作?

Here is my code. 这是我的代码。

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))
  });
}

Indeed GET requests can't have a body, meaning you don't send data while you are getting. 确实,GET请求不能具有主体,这意味着您在获取数据时不会发送数据。 There can be two things going on here. 这里可能有两件事。

  1. That specific endpoint is supposed to use another method like POST 该特定端点应该使用其他方法,例如POST
  2. The data you want to send actually needs to be passed as querystring parameter http://api.pixelz.com/REST.svc/Templates/?contactAPIkey=....&contactEmail=... 实际上,您要发送的数据需要作为querystring参数http://api.pixelz.com/REST.svc/Templates/?contactAPIkey=....&contactEmail=...

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM