从api调用获取响应,但在response.json()中获取错误-未捕获(已承诺)SyntaxError:JSON中位置4处的标记<

时间:2019-05-07 08:56:53

标签: javascript json reactjs api response

我正在尝试使用客户端ID和使用GET方法以及“ multiform / form-data”内容类型的密钥来获取访问令牌。我的响应状态为200,但是当我返回response.json()时出现错误。

 getDevice() {
var url =
  "https://login.windows.net/4c70fa10-6797-45e2-9760- 22663f35a3bd/oauth2/token?api-version=1.6";
var datastr =
  "grant_type:client_credentials,client_id:96b8db04-176d-4582-9101-a2f0869edff5,client_secret:5P453fshsj*_lYzkgDJSTA_UVmaI-JHf,resource:https://graph.windows.net";
fetch(
  url,
  {
    method: "GET",
    headers: {
      "Content-Type": "multipart/form-data",
      Accept: "application/json"
    }
  },
  datastr
)
  .then(response => {
    console.log(response);

    return response.json();
  })
  .then(myJson => {
    console.log(myJson);
    this.setState({
      device_token: myJson.access_token
    });
    console.log(myJson.access_token);
    console.log(this.state.device_token);
  });

}

                    OUTPUT IN CONSOLE

响应{type:“ cors”,url:“ https://login.windows.net/4c70fa10-6797-45e2-9760-22663f35a3bd/oauth2/token?api-version=1.6”, 重定向:false,  状态:200, 好的:是的,…} 正文:(...)bodyUsed:trueheaders:标头{} ok:true重定向:falsestatus:200statusText:“ OK”类型:“ cors” url:“ https://login.windows.net/4c70fa10-6797-45e2-9760-22663f35a3bd/oauth2/token?api-version=1.6 proto :响应< / p>

users:1未捕获(承诺)语法错误:JSON中位置4的意外令牌<< / p>

0 个答案:

没有答案