gpt4 book ai didi

javascript - 如何通过ajax传递json数据?

转载 作者:行者123 更新时间:2023-11-28 01:46:43 27 4
gpt4 key购买 nike

我正在开发一个 Visual Studio 2012 MVC 程序。

我使用ajax将数据发送到 Controller ,并希望 Controller 返回html正文。数据为json格式。数据是字符串名称和十进制 TotFees。

我发现 Controller 中的public ActionResult ImmPay(string Name)中的参数值始终为空。最后我尝试只传递名称,但 Controller 端的名称值仍然为空。

我的代码有什么问题,如何解决这个问题?谢谢。

View:
function ImmPay()
{
var name = "ASP";
var TotFees = 100.01;

//var dd = "{\'name\':\'" + name + "\', \'TotFees\':\'" + TotFees + "\'}";
//var dd = "{\'name\':\'" + name + "\', \'TotFees\':\'" + TotFees + "m\'}";

dd = "{\'b\':\'" + b + "\'}";
dd = JSON.stringify(dd);
$.ajax({
url: '@Url.Action("ImmPay", "Consult")',
type: 'GET',
async: true,
data: dd,
contentType: 'application/json',
context: document.body,
success: function (response, textStatus, jqXHR) {
$("#dialog-immpay").html(response);
$("#dialog-immpay").dialog("open");
},
error: function (jqXHR, textStatus, errorThrown) {
alert(textStatus);
},
complete: function () {
;
}
});

}

Controller:
public ActionResult ImmPay(string Name)
{
do something here
}

最佳答案

JSON.stringify 接受一个对象或数组并将其转换为 JSON,因此您可以将数据构建到一个对象中并像这样对其进行字符串化

dd = JSON.stringify({b: b});

关于javascript - 如何通过ajax传递json数据?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20205104/

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