解析HttpClientRequest响应

时间:2014-11-18 11:07:50

标签: javascript post httpclient

我还有另一个新手(可能是愚蠢的)问题。我正在使用HttpClientRequest并进行调用。我也有回应。

var url = <my url>
var request = new HttpClientRequest(url);
request.header["Content-Type"] = "application/x-www-form-urlencoded";
request.method = "POST";

try
{

  request.execute();
  var rawResponse = request.response.body.toString();
}

来自服务器的响应采用以下格式: {&#34;令牌&#34;:&#34; ABC&#34;&#34; expires_in&#34;:9292,&#34;刷新&#34;:&#34; deeDfTTgendj&#34;}

我只需要提取&#34; expires_in&#34;和&#34;刷新&#34;响应中的字段

1 个答案:

答案 0 :(得分:0)

由于这是有效的JSON,您可以解析它:

var rawResponse = request.response.body.toString(),
    objectLiteral = JSON.parse(rawResponse);

var expires_in = objectLiteral['expires_in'],
    refresh = objectLiteral['refresh'];

var rawResponse = '{"token":"abc","expires_in":9292,"refresh":"deeDfTTgendj"}';
    objectLiteral = JSON.parse(rawResponse);

var expires_in = objectLiteral['expires_in'],
    refresh = objectLiteral['refresh'];

console.log(expires_in, refresh);

注意:查看browser support

JSON.parse()