2016-12-30 111 views
0

我能夠從server{"html":"response header <!doctype html>MY Intrest is this HTML Block<html></html>"}如何從Ajax響應取出頭

得到響應以這種方式我想刪除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>

+0

你想提取字符串「<!DOCTYPE HTML>這裏內容goges」 – codeMan

+0

@codeMan,你讓我找到這就是正是我要找的 –

+0

這是一個有效的問題爲什麼要投票?'任何原因 –

回答

4

嘗試slice()方法

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);

+0

誰是你男人多數民衆贊成偉大的答案 –

+0

很高興幫助。這裏只是基本的JavaScript – anu

1

一種方式來做到這一點是通過搜索<html><!doctype html>(如果你想包含)來自服務器的響應。

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>")); 
} 

現在調用這個函數,它將只返回HTML

var data = get_only_html(response); 

可以使用search()代替indexof但會execute slower as it expects regular expression.

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));

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]