Dilip G
Dilip G

Reputation: 529

how to take out header from ajax response

I'm able to get response in this fashion from server {"html":"response header <!doctype html>MY Intrest is this HTML Block<html></html>"}

What I want to remove response header which looks something like this HTTP\/1.1 200 OK\r\nDate till it finds <!doctype html>

here is my sample data:

{"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>"}

How can I remove this response header?

My expected output: <!doctype html><html>content goges here</html>

Upvotes: 0

Views: 415

Answers (3)

Nadir Laskar
Nadir Laskar

Reputation: 4150

One way to do it is by searching the <html> or <!doctype html> (if you want to include) from the server response.

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

Now call this function and it will return only html

var data = get_only_html(response);

You can use search() instead of indexof but will 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));

Upvotes: 1

gvmani
gvmani

Reputation: 1600

Use regex to extract the part you need. "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]

Upvotes: 0

Anupam
Anupam

Reputation: 8016

Try slice() method

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

Upvotes: 3

Related Questions