关于javascript:如何从ajax响应中取出标头

how to take out header from ajax response

我能从server{"html":"response header MY Intrest is this HTML Block"}得到这种反应。

我要删除的东西是response header,它看起来像这个HTTP\/1.1 200 OK

Date,直到它找到

以下是我的示例数据:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
{"html":"HTTP\/1.1 200 OK

Date: Fri, 30 Dec 2016 07:33:08 GMT

Expires: -1

Cache-Control: private, max-age=0

Content-Type: text\/html; charset=ISO-8859-1

P3P: CP="
This is not a P3P policy! See https:\/\/www.google.com\/support\/accounts\/answer\/151657?hl=en for more info."

Server: gws

X-XSS-Protection: 1; mode=block

X-Frame-Options: SAMEORIGIN

Set-Cookie: NID=93=ntWba1QpElyHkWL02fhHB6zujRcM8Sb6dfSww39vgUSaNOg6ya2e6PCZ2BA1zYhP2w9qeCy8X416oWKhHKfBAa17kEgNiRoSU0kMbRfuyVtahTUYDpCqS7knxRPhYSyei3MpXIakXU9BepA; expires=Sat, 01-Jul-2017 07:33:08 GMT; path=\/; domain=.google.com; HttpOnly

Accept-Ranges: none

Vary: Accept-Encoding

Transfer-Encoding: chunked



<!doctype html><!-- THIS WHAT I WANT --><\/html>"
}

我怎样才能取下这个response header

我的预期产量:content goges here


试用slice()

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
var response = {"html":"HTTP\/1.1 200 OK

Date: Fri, 30 Dec 2016 07:33:08 GMT

Expires: -1

Cache-Control: private, max-age=0

Content-Type: text\/html; charset=ISO-8859-1

P3P: CP="
This is not a P3P policy! See https:\/\/www.google.com\/support\/accounts\/answer\/151657?hl=en for more info."

Server: gws

X-XSS-Protection: 1; mode=block

X-Frame-Options: SAMEORIGIN

Set-Cookie: NID=93=ntWba1QpElyHkWL02fhHB6zujRcM8Sb6dfSww39vgUSaNOg6ya2e6PCZ2BA1zYhP2w9qeCy8X416oWKhHKfBAa17kEgNiRoSU0kMbRfuyVtahTUYDpCqS7knxRPhYSyei3MpXIakXU9BepA; expires=Sat, 01-Jul-2017 07:33:08 GMT; path=\/; domain=.google.com; HttpOnly

Accept-Ranges: none

Vary: Accept-Encoding

Transfer-Encoding: chunked



<!doctype html><!-- THIS WHAT I WANT --><\/html>"
};
var sliced = response.html.slice(response.html.search('<!doctype html>'));
console.log(sliced);


一种方法是从服务器响应中搜索(如果您想包括)。

埃多克斯1〔2〕

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

现在调用这个函数,它只返回HTML

1
var data = get_only_html(response);

您可以使用search()而不是indexof,但执行速度会比预期的正则表达式慢。

1
2
3
4
5
6
7
8
var response = {"html":"response header <!doctype html>MY Intrest is this HTML Block<html></html>"}

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

  console.log(get_only_html(response));


使用regex提取所需的零件。埃多克斯1〔6〕

江户十一〔七〕号