如何正确转义 jQuery 的 .ajax 函数中作为数据发送的 html

发布于 2024-10-01 02:35:44 字数 1286 浏览 0 评论 0原文

更新:当我查看 Firebug 中的问题时,我立即发现了我的错误。这是一个令人尴尬的、无与伦比的双引号,我一定是以某种方式删除了它。我一直在使用 Chrome 的开发者窗口。非常抱歉用尽了您的资源。但是,吸取教训了! (“我希望。)

对我来说,转义要发送到服务器的 html 字符的最佳方法是什么?我正在使用 jQuery、.ajax() 和 jsonp。

我正在编写一个书签,它发送部分当前页面的 html 到我的服务器。这是 ajax 调用:

jQuery.ajax({
    url: 'http://www.my_server.com/file.php?callback=?',
    dataType: 'jsonp',
    data: { someHtml: escape(jQuery(this).html().substring(0,1000)) },
    success: function() { // stuff },
    beforeSend: function(xhr) {
                  xhr.setRequestHeader('Content-type','text/html');
                },
    error: function() { // stuff }
});

我需要使用 JSONP,因此我不能使用 POST,这就是为什么我要截断 html 数据,但如果 html 是“好的”,那么事情就可以了。如果它包含 javascript 不喜欢的字符,那么我有问题。我通过使用 escape() 解决了我的问题,但现在我认为我遇到了换行符和制表符问题,

给了我同样的错误:

Uncaught SyntaxError: Unexpected token <

这 假设意味着某些字符导致 JavaScript 崩溃。我尝试了以下方法:escape()、encodeURI/Component()、serialize()、text(),但一开始我没有任何效果。使用 beforeSend,但我想我应该尝试一下,但没有什么区别。

目前,我遇到了一些带有换行符、制表符和几个空格的 html,我尝试使用 Replace() 替换这些字符:

... .substring(0,1000).replace(/(\r\n|[\r\n])/g,'')

我在另一个网站上发现了这个正则表达式字符串,它应该替换回车符和换行符的各种组合。

我希望我已经把自己解释得足够清楚了。这是我在 Stack Overflow 上提出的第一个问题,所以请放轻松。 :)

UPDATE: Once I looked at the problem in Firebug, I found my mistake immediately. And it was an embarrassing unmatched double quote that I must have deleted somehow. I had been using Chrome's developer window. Very sorry for using up your resources. But, lesson learned! ("I hope.)

What is the best way for me to escape html characters that I want to send to my server? I am using jQuery, .ajax(), and jsonp.

I'm writing a bookmarklet which sends parts of the current page's html to my server. Here is the ajax call:

jQuery.ajax({
    url: 'http://www.my_server.com/file.php?callback=?',
    dataType: 'jsonp',
    data: { someHtml: escape(jQuery(this).html().substring(0,1000)) },
    success: function() { // stuff },
    beforeSend: function(xhr) {
                  xhr.setRequestHeader('Content-type','text/html');
                },
    error: function() { // stuff }
});

I need to use JSONP and therefore I can't use POST, and this is why I'm truncating the html data. Things work if the html is "nice", but if it contains characters javascript doesn't like, then I have problems. I fixed my ' problem by using escape(), but now I think I'm having newline and tab problems.

Chrome's dev console gives me the same error:

Uncaught SyntaxError: Unexpected token <

which I assume means some character is causing things to break out of javascript. I have tried the following: escape(), encodeURI/Component(), serialize(), text(), but nothing has worked yet. At first, I didn't use beforeSend, but thought I should try it, but no difference.

Currently, I'm stuck with some html which has a line break, then a tab, then a couple of spaces. I have tried replacing these characters using replace():

... .substring(0,1000).replace(/(\r\n|[\r\n])/g,'')

I found this regex string on another site which is supposed to replace various combinations of carriage returns and line feeds.

I hope I've explained myself clearly enough. It's my first question at Stack Overflow so go easy on me. :)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

你丑哭了我 2024-10-08 02:35:44

您不需要转义或编码。 jQuery 将负责对数据进行正确的 URL 编码:

data: { someHtml: $(this).html().substring(0, 1000) },

You don't need to escape or encode. jQuery will take care of properly URL encoding the data:

data: { someHtml: $(this).html().substring(0, 1000) },
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文