ASP.NET MVC + jQuery - 当我通过 jQuery $.post 表单时,如何保留 .NET 行结尾?
我的 jQuery 代码的作用如下:
$('.newCommentBox form').live('submit', function () {
//Submit the form post
$.post($(this).attr('action'), $(this).serialize(), function (data) { showNewComment(data) });
return false;
});
每当提交表单时,我都会获取结果,将其序列化,然后通过 POST 将它们发送到特定操作。当我们从服务器收到响应时,我调用 showNewComment 函数来实时显示正在添加的评论。
这是我的问题:我需要能够保留人们在评论中留下的空白,以便他们可以控制较长评论的可读性。当渲染注释时,我使用 .NET Html 辅助方法将 CRLF 换行符转换为
标记,但是当通过 jQuery 提交表单时,所有这些行结尾都不会保留并通过 JSON 进行序列化,而当我在没有 AJAX 的情况下执行此操作时,它工作得很好。
我如何保留这些行结尾,或者 jQuery 用什么替换了 CRLF 行结尾?
Here is what my jQuery code is doing:
$('.newCommentBox form').live('submit', function () {
//Submit the form post
$.post($(this).attr('action'), $(this).serialize(), function (data) { showNewComment(data) });
return false;
});
Whenever a form is submitted, I take the results, serialize them, and send them to the specific action via a POST. When we get a response back from the server, I invoke the showNewComment function to display the comment being added in real-time.
Here is my issue: I need to be able to preserve the whitespace people leave in their comments so they can control the readability of longer comments. I use a .NET Html helper method to convert CRLF linebreaks into <br/>
tags when the comments get rendered, but all of those line-endings are not preserved when the form is submitted via jQuery and serialized via JSON, whereas when I did this without AJAX it worked just fine.
How can I preserve these line endings, or what did jQuery replace the CRLF line-endings with?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
使用ajax,我看到根据浏览器和环境,换行符被发布为 \r\n 和 \n ,但总的来说,我发现检查 \n 并将其替换为
< /code> 是最安全的选择。
例如在 C# 中:
Using ajax, I've seen line breaks being posted as \r\n and \n depending on browser and environment, but in general, I found that checking for \n and replace it with
<br />
is the safest bet.For example in C#:
另一种方法是使用 CSS 空白属性来设置输出的样式。
应呈现为
保留制表符和行距
请参阅 http://www .w3.org/TR/CSS2/text.html#white-space-prop 了解更多信息
An alternative is to use the CSS white-space property to style your output.
should be rendered as
with tabs and linespacing preserved
See http://www.w3.org/TR/CSS2/text.html#white-space-prop for more info