gpt4 book ai didi

c# - 从 JQuery 将 JSON 发布到 ASP.NET MVC 4 操作

转载 作者:太空狗 更新时间:2023-10-29 19:53:50 25 4
gpt4 key购买 nike

我正在开发一个使用 ASP.NET MVC 4 的应用程序。在某些方面,我觉得我正在从头开始学习一切:)。有人告诉我这是值得的。

我需要将一些 JSON 发送到 Controller 中的操作。我的操作如下所示:

[Authorize]
public class MyController : Controller
{
[HttpPost]
public ActionResult RemoveItem(string itemID)
{
// Do stuff...
return Json(new { Status = 1, Message="Success" });
}
}

我的 JQuery 代码如下所示:

function removeItem(id) {
var json = { "itemID": id };
$.ajax({
type: "POST",
url: "/myController/removeItem",
contentType: "application/json; charset=utf-8",
data: json,
dataType: "json",
success: removeItemCompleted,
error: removeItemFailed
});
}

function removeItemCompleted(results) {
}

function removeItemFailed(request, status, error) {
}

在 Fiddler 中,我注意到返回了 500 错误。响应中的 TITLE 字段显示:“无效的 JSON 原语:itemID”。

我做错了什么?

谢谢!

最佳答案

请务必发送 JSON:

data: json,

应该是

data: JSON.stringify(json),

IE7 及以下版本需要垫片:https://github.com/douglascrockford/JSON-js

注意:Dave A 的回答也是正确的,但没有直接回答您的问题。我对其 +1。

关于c# - 从 JQuery 将 JSON 发布到 ASP.NET MVC 4 操作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14752522/

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