问题描述
更新:有一次,我在Firebug看了看问题,我马上发现我的错误。它是我必须莫名其妙地删除了一个尴尬的无与伦比的双引号。我使用Chrome的开发窗口已经过。很抱歉,用你的资源。但是,经验教训! (我希望。)
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.)
什么是我要逃跑,我要发送到我的服务器HTML字符的最佳方式?我使用jQuery,阿贾克斯(),和JSONP。
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部分到我的服务器一个书签。下面是Ajax调用:
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 }
});
我需要使用JSONP,所以我不能使用POST,这就是为什么我要截断的HTML数据。物联网工作,如果HTML是好,但如果它包含JavaScript不喜欢的人物,那么我有问题。我已修正问题,通过使用转义(),但现在我觉得我有换行和标签的问题。
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的开发控制台给了我同样的错误:
Chrome's dev console gives me the same error:
Uncaught SyntaxError: Unexpected token <
我假设意味着一些性格造成的东西打出来的JavaScript。我曾尝试以下操作:越狱(),EN codeURI /组件(),序列化(),文本(),但没有奏效呢。起初,我没有使用beforeSend,但认为我应该尝试,但没有什么区别。
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.
目前,我只能和一些HTML里面有一个换行符,然后选项卡,然后一对夫妇的空间。我曾尝试使用替换替换这些字符():
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. :)
推荐答案
您不必逃避或EN code。 jQuery将照顾正确的URL编码数据:
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) },
这篇关于如何正确逃生HTML中jQuery的阿贾克斯函数发送数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!