1

我在 SO 上阅读了许多关于用 javascript 替换反斜杠的问题并尝试了它们,但它们对我不起作用。响应文本包含反斜杠,我需要摆脱它们但没有任何效果?这是发出请求的 JS 函数:

  requestKittens: function() {
    var req = new XMLHttpRequest();

    req.onreadystatechange = function (event) {
    var xhr = event.target;


    if (xhr.readyState === 4 && xhr.status === 200) {
    var str = xhr.responseText;
    str.replace(/\\/g, '');
     document.getElementsByTagName('body')[0].innerHTML = xhr.responseText

    }
};

    req.open('GET', this.searchOnFlickr_, true);
        req.setRequestHeader('accessKey','fdsfkslfjslfjlgfdgdfg');


    req.send();
  },

这是来自服务器的响应

{"topics":[{"topicLabel":"计算机概念","topicThesaurusName":"topics","topicUrl":" http://dictionary.cambridge.org/topics/technology/computer-concepts/","topicId":"computer-concepts","topicParentId":"technology"}],"dictionaryCode":"british","entryLabel":"address noun (COMPUTERS)","entryContent":"address</ h1> 名词</span> [C</span></span>]</span></span>(计算机)</span>https://dictionary.cambridge.org/external/images/uk_pron.png ?version=2013-08-07-1929\"/></a>https://dictionary.cambridge.org/media/british/uk_pron/u/uka/ukada/ukadapt021.mp3\"/>https:/ /dictionary.cambridge.org/media/british/uk_pron_ogg/u/uka/ukada/ukadapt021.ogg\"/>您的浏览器不支持HTML5音频。</audio>https://dictionary.cambridge.org/external/图片/us_pron.png?version=2013-08-07-1929\"/></a>https://dictionary.cambridge.org/media/british/us_pron/e/eus/eus70/eus70074.mp3\"/>https://dictionary.cambridge.org/media/british/us_pron_ogg/e/eus/eus70/eus70074.ogg\"/>您的浏览器不支持 HTML5 音频。</audio> /əˈdres</span>/</span> /ˈæd.res</span>/</span> </span></header>A1</span> < /span>一系列字母和符号,它们告诉您在 Internet 上的何处可以找到某些内容或显示电子邮件的发送地址:</span></span>您的电子邮件地址是什么?</span></span>做你有他们的网址吗?</span></span></section> \u203a</span> 专门的</span></span></span> 一段信息存储在计算机内存中的地方</span></span></section>(地址名词(计算机)的定义来自 https://dictionary.cambridge.org/dictionary/british/\" title=\"Cambridge Advanced Learner's Dictionary & Thesaurus\n\">Cambridge Advanced Learner's Dictionary & Thesaurus\n< /a> © 剑桥大学出版社)</small></footer></article>","entryUrl":"http://dictionary.cambridge.org/dictionary/british/address_2 ","格式":"html","entryId":"address_2"}

什么可能导致问题?

4

1 回答 1

1

尝试:

str = str.replace(/\\/g, '');

和:

document.getElementsByTagName('body')[0].innerHTML = str;
于 2013-08-31T10:58:08.150 回答