我正在使用AngularJS和Cloudant构建一个应用程序,当我包含一个新文档时,回调中包含响应中的修订号,但它们都是一个大字符串,如下所示:
HTTP/1.1 201 Created
X-Frame-Options: DENY
X-Couch-Request-ID: cf04aa4a96
Server: CouchDB/2.1.1 (Erlang OTP/17)
Location: https://xxx.cloudant.com/xxx/xxx/1e829630-cdb6-463a-8c25-9cc140ff8ab0
Date: Wed, 04 Apr 2018 14:15:59 GMT
Content-Type: application/json
Content-Length: 99
Cache-Control: must-revalidate
Strict-Transport-Security: max-age=31536000
X-Content-Type-Options: nosniff
X-Cloudant-Request-Class: write
X-Cloudant-Backend: bm-cc-uk-04
Via: 1.1 lb1.bm-cc-uk-04 (Glum/1.50.4)
{"ok":true,"id":"1e829630-cdb6-463a-8c25-9cc140ff8ab0","rev":"1-05a055a4f2e4baf71b46a14386eb7fab"}
如何从此字符串中提取rev并将其保存在变量中?
我已经尝试了这个,但它不起作用,因为响应是一个包含对象的字符串......
$promisedb.then(function (datawop) {
$scope.rev = datawop.data.rev;
}
答案 0 :(得分:1)
HTTP标头由序列\r\n\r\n
(空行)终止。
$promisedb.then(function (response) {
var splitArray = response.data.split("\r\n\r\n");
var headers = splitArray[0];
var body = splitArray[1];
var data = JSON.parse(body);
$scope.rev = data.rev;
}