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

时间:2021-08-21 22:29:27

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

更新:一旦我在Firebug中查看问题,我立即发现了我的错误。这是一个令人尴尬的无与伦比的双引号,我必须以某种方式删除。我一直在使用Chrome的开发者窗口。很抱歉用完你的资源。但是,经验教训! (“我希望。)

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.

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

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

我正在写一个bookmarklet,它将当前页面的部分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 }
});

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.

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

Chrome's dev console gives me the same error:

Chrome的开发控制台给了我同样的错误:

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.

我认为这意味着某些角色会导致事情突破javascript。我尝试了以下:escape(),encodeURI / Component(),serialize(),text(),但还没有任何工作。起初,我没有使用beforeSend,但我想我应该尝试一下,但没有区别。

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():

目前,我遇到了一些html,它有一个换行符,然后是一个标签,然后是几个空格。我尝试使用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. :)

我希望我已经足够清楚地解释了自己。这是我在Stack Overflow上的第一个问题,所以对我来说很容易。 :)

1 个解决方案

#1


6  

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

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

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

#1


6  

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

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

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