此代码无需发送参数即可运行:
$(function () {
$('#Fee').on('focus', function () {
$.ajax({
url: '@Url.Action("GetFee")',
dataType: "json",
type: "POST",
contentType: 'application/json; charset=utf-8',
cache: false,
data: { },
success: function (data) {
if (data.success) {
$('#Fee').val(data.message);
}
}
});
});
});
但是,如果我想将参数发送到 GetFee
操作方法,它就不再起作用了:
data: { bookname : 'book1' }
然后我更改了我的操作方法以接受参数:
[HttpPost]
public ActionResult GetFee(string bookname)
最佳答案
您指出:
contentType: 'application/json; charset=utf-8',
因此请确保您尊重您声称要发送到服务器的内容:
data: JSON.stringify({ bookname : 'book1' })
另一方面,如果你在你的请求中去掉这个application/json
内容类型,jQuery将使用application/x-www-form-urlencoded
默认然后你可以简单地使用这个:
data: { bookname : 'book1' }
关于c# - 为什么我不能使用 ajax 将参数发送到操作方法?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41759066/