如何从ajax响应中取出标头

时间:2016-12-30 09:01:57

标签: javascript jquery node.js

我可以通过server {"html":"response header <!doctype html>MY Intrest is this HTML Block<html></html>"}

以这种方式获得回复

我想删除response header看起来像HTTP\/1.1 200 OK\r\nDate的内容,直到找到<!doctype html>

这是我的样本数据:

{"html":"HTTP\/1.1 200 OK\r\nDate: Fri, 30 Dec 2016 07:33:08 GMT\r\nExpires: -1\r\nCache-Control: private, max-age=0\r\nContent-Type: text\/html; charset=ISO-8859-1\r\nP3P: CP=\"This is not a P3P policy! See https:\/\/www.google.com\/support\/accounts\/answer\/151657?hl=en for more info.\"\r\nServer: gws\r\nX-XSS-Protection: 1; mode=block\r\nX-Frame-Options: SAMEORIGIN\r\nSet-Cookie: NID=93=ntWba1QpElyHkWL02fhHB6zujRcM8Sb6dfSww39vgUSaNOg6ya2e6PCZ2BA1zYhP2w9qeCy8X416oWKhHKfBAa17kEgNiRoSU0kMbRfuyVtahTUYDpCqS7knxRPhYSyei3MpXIakXU9BepA; expires=Sat, 01-Jul-2017 07:33:08 GMT; path=\/; domain=.google.com; HttpOnly\r\nAccept-Ranges: none\r\nVary: Accept-Encoding\r\nTransfer-Encoding: chunked\r\n\r\n<!doctype html><!-- THIS WHAT I WANT --><\/html>"}

如何删除此response header

我的预期输出:<!doctype html><html>content goges here</html>

3 个答案:

答案 0 :(得分:3)

尝试slice()方法

&#13;
&#13;
var response = {"html":"HTTP\/1.1 200 OK\r\nDate: Fri, 30 Dec 2016 07:33:08 GMT\r\nExpires: -1\r\nCache-Control: private, max-age=0\r\nContent-Type: text\/html; charset=ISO-8859-1\r\nP3P: CP=\"This is not a P3P policy! See https:\/\/www.google.com\/support\/accounts\/answer\/151657?hl=en for more info.\"\r\nServer: gws\r\nX-XSS-Protection: 1; mode=block\r\nX-Frame-Options: SAMEORIGIN\r\nSet-Cookie: NID=93=ntWba1QpElyHkWL02fhHB6zujRcM8Sb6dfSww39vgUSaNOg6ya2e6PCZ2BA1zYhP2w9qeCy8X416oWKhHKfBAa17kEgNiRoSU0kMbRfuyVtahTUYDpCqS7knxRPhYSyei3MpXIakXU9BepA; expires=Sat, 01-Jul-2017 07:33:08 GMT; path=\/; domain=.google.com; HttpOnly\r\nAccept-Ranges: none\r\nVary: Accept-Encoding\r\nTransfer-Encoding: chunked\r\n\r\n<!doctype html><!-- THIS WHAT I WANT --><\/html>"};
var sliced = response.html.slice(response.html.search('<!doctype html>'));
console.log(sliced);
&#13;
&#13;
&#13;

答案 1 :(得分:1)

一种方法是从服务器响应中搜索keytool -genkey -v -keystore my-release-key.keystore -alias alias_name -keyalg RSA -keysize 2048 -validity 10000 <html>(如果要包括)。

<!doctype html>

var response = {"html":"response header <!doctype html>MY Intrest is this HTML Block<html></html>"}

现在调用此函数,它将只返回html

function get_only_html(response){
    html_content = response.html;
    return html_content.slice(html_content.indexOf("<!doctype html>"));
}

您可以使用var data = get_only_html(response); 代替search(),但execute slower as it expects regular expression.

indexof

答案 2 :(得分:0)

使用正则表达式提取所需的部分。 "sdfdd".match(/.html>(.)<\/html>$/)[1]

'HTTP\/1.1 200 OK\r\nDate: Fri, 30 Dec 2016 07:33:08 GMT\r\nExpires: -1\r\nCache-Control: private, max-age=0\r\nContent-Type: text\/html; charset=ISO-8859-1\r\nP3P: CP=\"This is not a P3P policy! See https:\/\/www.google.com\/support\/accounts\/answer\/151657?hl=en for more info.\"\r\nServer: gws\r\nX-XSS-Protection: 1; mode=block\r\nX-Frame-Options: SAMEORIGIN\r\nSet-Cookie: NID=93=ntWba1QpElyHkWL02fhHB6zujRcM8Sb6dfSww39vgUSaNOg6ya2e6PCZ2BA1zYhP2w9qeCy8X416oWKhHKfBAa17kEgNiRoSU0kMbRfuyVtahTUYDpCqS7knxRPhYSyei3MpXIakXU9BepA; expires=Sat, 01-Jul-2017 07:33:08 GMT; path=\/; domain=.google.com; HttpOnly\r\nAccept-Ranges: none\r\nVary: Accept-Encoding\r\nTransfer-Encoding: chunked\r\n\r\n<!doctype html><!-- THIS WHAT I WANT --><\/html>'.match(/.*html>(.*)<\/html>$/)[1]