gpt4 book ai didi

c# - 将字符串从 jQuery 传递到 C# MVC

转载 作者:行者123 更新时间:2023-11-30 23:32:06 25 4
gpt4 key购买 nike

我在将字符串值从 jQuery 传递到 Web MVC 中的 C# 时遇到问题。

这是我的 jQuery 代码:

$.ajax({
type: 'POST',
url: '/Account/ChangePhoneNumber',
contentType: "application/json; charset=utf-8",
data: newPhone,
dataType: 'json',
success: function (result) {
alert("We returned: " + result);
}
})

这是我的 C# 方法:

[HttpPost]
public ActionResult ChangePhoneNumber(string data)
{
return View();
}

调试时 data 参数始终为 null。我做错了什么?

最佳答案

好的,我已经知道出了什么问题:

 $.ajax({
type: 'POST',
url: '/Account/ChangePhoneNumber',
contentType: "application/json; charset=utf-8",
data: JSON.stringify({data: newPhone}),
dataType: 'json',
success: function (result) {
alert("We returned: " + result);
}
})

我必须添加数据:JSON.stringify({data: newPhone}),

关于c# - 将字符串从 jQuery 传递到 C# MVC,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34413013/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com