2016-12-30 2 views
0

Я могу получить ответ таким образом от 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>

+0

Вы хотите извлечь строку « 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]

Смежные вопросы