【发布时间】:2016-10-21 16:42:15
【问题描述】:
我在一个 mvc 程序上工作,单击一个按钮,我将 TextArea 内的数据发送到控制器。通常,我的代码按预期工作,但当 TextArea 内的数据由基于 html 的数据组成时,情况并非如此。
HTML代码:
<textarea id="bodyInfo"> </textarea>
<button onclick="Submit()" id="submitInfo">Create Notification</button>
ajax:
function Submit() {
$.ajax({
url: '@Url.Action("GetResults", "notification")',
type: 'GET',
data: { body: $('#bodyInfo').val()),
cache: 'false',
dataType: 'html',
success: function (result) {
$('#resultsTblInfo').html(result);
}
});
return false;
}
TextArea 数据示例:
<table style="width:100%">
<tbody>
<tr>
<td class="ellipses-title" style="color:#22557f;font-size:15px;font-weight:bold;margin-bottom:10px;margin-top:7px;border-right:1px solid #BFF1FD;text-align:right;padding-right:15px;"> The New Admin is Coming</td>
<td class="ellipses-text" style="padding-left:15px;" valign="center">Hello<br> Hello2</a>.
</td>
</tr>
</tbody>
</table>
当 TextArea 不是 html 数据而是原始文本时,上面看到的内容会起作用,但当它是 html 数据时会失败。
我能够通过使用来完成这项工作
“encodeURIComponent($('#bodyInfo').val())” 而不是“$('#bodyInfo').val()”
在控制器端,做
"body = HttpUtility.UrlDecode(body)";
是否有更好的选择来实现相同的目标?我是否在违背其预期性质使用 encodeURIComponent ?为什么通过ajax传递html值时$('#bodyInfo').val()不起作用?如果这个问题是重复的,如果有人能给我一个链接,我将不胜感激(我尝试通过谷歌搜索但没有找到令人满意的答案)
【问题讨论】:
标签: javascript jquery html ajax