jQuery AJAX 正在从 HTML 数据中剥离内联 CSS
我正在尝试通过 AJAX 将 HTML 数据传递到服务器。我最终希望将完整的 HTML 代码(包括任何内联 CSS 定义)存储在数据库中。我不想对 HTML 数据进行任何更改,只需按原样存储即可。我的问题是,当我通过 AJAX 传递 HTML 数据时,当它到达服务器时,内联 CSS 已被删除,但不仅如此,它还被错误地删除,留下了无效的 HTML。
这是我想要做的一个示例:
var the_html = {'html':'<p><span style="font-size:42px;"><span style="color:#f00;">
Some text</span></span></p>'};
$.ajax({
type: 'POST',
url: 'ajax.php',
data: the_html,
success: function (data) {
alert(data);
}
});
当数据到达服务器时,它看起来像这样:
<p><span>span style="color:#f00;">Some Text</span></span></p>
在我看来,jQuery 试图剥离所有内联 CSS,但也搞砸了第一个 span 标签删除第二个 span 标签的左尖括号。我尝试在通过 AJAX 发送数据之前转义数据,但结果是相同的。另外,我使用对象文字语法是因为我要传递的数据比示例中多得多,并且我希望它在到达服务器时成为关联数组。
我根本不想删除内联 CSS。我希望所有标记加上任何 CSS 都完好无损地发送到服务器。关于如何实现这一目标有什么想法吗?
I am trying to pass HTML data to the server via AJAX. I ultimately want to store the full HTML code, including any inline CSS definitions, in a database. I don't want to make any changes to the HTML data, just store it as-is. My problem is that when I pass HTML data through AJAX, when it reaches the server the inline CSS has been stripped out, but not only that, it was stripped out incorrectly leaving invalid HTML behind.
Here's a sample of what I'm trying to do:
var the_html = {'html':'<p><span style="font-size:42px;"><span style="color:#f00;">
Some text</span></span></p>'};
$.ajax({
type: 'POST',
url: 'ajax.php',
data: the_html,
success: function (data) {
alert(data);
}
});
When the data reaches the server it looks like this:
<p><span>span style="color:#f00;">Some Text</span></span></p>
It seems to me like the jQuery tried to strip all of the inline CSS out, but screwed up with the first span tag by also removing the opening angle bracket of the second span tag. I tried escaping the data before sending it through AJAX, but the results were the same. Also, I am using the object literal syntax because I have a lot more data to pass than in the example and I want it to be an associative array when it gets to the server.
I don't want the inline CSS stripped out at all. I want all of the markup plus any CSS to be sent to the server intact. Any ideas on how I can accomplish this?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
在 javascript/frontend 上将 style 替换为 style1(或任何内容)
并再次在服务器端将其从 style1 替换回 style
example:
在服务器端(对我来说是 PHP):
replace style with style1( or anything) on javascript/frontend
and again on server-side replace it back from style1 to style
example:
On serverside(its PHP for me):