【问题标题】:how to take out header from ajax response如何从ajax响应中取出标头
【发布时间】:2017-05-14 14:08:57
【问题描述】:

我能够以这种方式从server{"html":"response header <!doctype html>MY Intrest is this HTML Block<html></html>"}得到回复

我想删除类似于 HTTP\/1.1 200 OK\r\nDateresponse header 直到找到 <!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

我的预期输出:&lt;!doctype html&gt;&lt;html&gt;content goges here&lt;/html&gt;

【问题讨论】:

  • 你想提取字符串“content goges here”
  • @codeMan,你说得对,这正是我要找的
  • 这是一个有效的问题,为什么down vote ? 任何原因

标签: javascript jquery node.js


【解决方案1】:

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

【讨论】:

  • 很高兴为您提供帮助。这里只是基本的javascript
【解决方案2】:

一种方法是从服务器响应中搜索&lt;html&gt;&lt;!doctype html&gt;(如果您想包含)。

var response = {"html":"response header &lt;!doctype html&gt;MY Intrest is this HTML Block&lt;html&gt;&lt;/html&gt;"}

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

【讨论】:

    【解决方案3】:

    使用正则表达式提取您需要的部分。 "sdfdd".match(/.html&gt;(.)&lt;\/html&gt;$/)[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&lt;!doctype html&gt;&lt;!-- THIS WHAT I WANT --&gt;&lt;\/html&gt;'.match(/.*html&gt;(.*)&lt;\/html&gt;$/)[1]

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-09-15
      • 1970-01-01
      • 1970-01-01
      • 2013-05-28
      • 2019-09-16
      • 1970-01-01
      • 2016-05-22
      • 1970-01-01
      相关资源
      最近更新 更多