【发布时间】:2021-01-12 20:50:53
【问题描述】:
我曾尝试查看类似问题的答案,但没有一个适用于我的代码。我尝试了很多不同的东西,它应该做的就是发布全名,然后将其显示回视图中。 查看代码:
<script type="text/javascript">
$(document).ready(function () {<script type="text/javascript">
$(document).ready(function () {
$('#buttonDemo2').click(function () {
var fullName = $('#fullName').val();
var payload = {fn : fullName};
$.ajax({
type: 'POST',
url: '/demo/demo2/',
contentType: 'application/json',
data: JSON.stringify(payLoad),
success: function (result) {
$('#result2').html(result);
}
});
});
</script>
<fieldset>
<legend>Demo 2</legend>
Full Name <input type="text" id="fullName" />
<input type="button" value="Demo 2" id="buttonDemo2" />
<br />
<span id="result2"></span>
</fieldset>
控制器代码:
[HttpPost]
public IActionResult Demo2(string fullName)
{
return new JsonResult("Hello " + fullName);
}
【问题讨论】:
-
您是否遇到任何错误?什么不起作用?
-
没有错误。它只返回“Hello”,但不会返回我在添加到它末尾的表单中输入的任何内容。例如。它的意思是说“你好鲍勃”。所以值永远不会到达控制器。
标签: c# ajax asp.net-mvc asp.net-core asp.net-ajax