Jquery 相互之间的Http请求(变量问题)

Jquery 相互之间的Http请求(变量问题),jquery,json,api,Jquery,Json,Api,我有一个http请求,通过令牌调用,然后解析该令牌并将其用于名为response的变量中。我希望在if命令的下一个http请求中使用该变量。不幸的是,调用变量的常规方法不起作用。有人能给我指一下正确的方向吗 我已经看到了反应,一切都很顺利。但是,变量response中的utoken没有读取输出 var xhr = new XMLHttpRequest(); xhr.open('POST', 'https://yotpoapi.apiary.io/oauth/token'); xhr.setReq

我有一个http请求,通过令牌调用,然后解析该令牌并将其用于名为
response
的变量中。我希望在if命令的下一个http请求中使用该变量。不幸的是,调用变量的常规方法不起作用。有人能给我指一下正确的方向吗

我已经看到了反应,一切都很顺利。但是,变量
response
中的utoken没有读取输出

var xhr = new XMLHttpRequest();
xhr.open('POST', 'https://yotpoapi.apiary.io/oauth/token');
xhr.setRequestHeader("Content-Type", "application/json");
xhr.onreadystatechange = function () {
  if (this.readyState == 4) {
    if (typeof cb !== "undefined") {
      cb(this);
    }
    else {
      var response = JSON.parse(this.responseText)

      var xhr = new XMLHttpRequest();
xhr.open('POST', 'https://yotpoapi.apiary.io/apps/BLAHBLAH/purchases');
xhr.setRequestHeader("Content-Type", "application/json");

xhr.send("{\n \"utoken\": \"response.access_token\"\n}");   

    }
  }
};
xhr.send("{\n   \"client_id\": \"CLIENT ID\",\n   \"client_secret\": \"CLIENT SECRET\",\n   \"grant_type\": \"client_credentials\"\n}");
输出:

"utoken": "response.access_token",
预期产出:

"utoken": "123123213123123123123123123",

您应该更改此行:

xhr.send("{\n \"utoken\": \"response.access_token\"\n}");
对此:

xhr.send("{\n \"utoken\": \"" + response.access_token + "\"\n}"); // Added additional quotes for response.access_token

原因是您发送的是
response.access\u-token
变量作为字符串,但应该作为变量包含在引号中。

我建议您查看
$.ajax()
JSON.stringify()
输出似乎是硬编码的:xhr.send({\n\'utoken\:\'response.access\u-token\'\n})@LarsJuelJensen我们输入带有动态变量的xhr.send。现在我们调用确保它的第一位是调用usertoken。但是,如何将该用户令牌(存储在
response
中)带到下一个函数中?您可能应该这样做:
xhr.send(“{\n\”utoken\:\”+response.access\u token+”\n}”)@JSG不客气:)请将我的答案标记为正确答案。