jquery - 从 MVC Controller 返回 JSON 字符串

标签 jquery asp.net-mvc json asp.net-mvc-4

我使用以下代码将对象发送/接收到我的 mvc Controller :

$.ajax({
url: _createOrUpdateTimeRecord,
data: JSON.stringify(data),
type: "POST",
//dataType: "json",
contentType: "application/json; charset=utf-8",
beforeSend: function () {
    $("#loading-overlay").show();
},
success: function (data2) {
    try {   // tried to parse it manually to see if anything changes.
        data2 = JSON.parse(data2);
    }
    catch (err) {

    }
},
error: function (xhr, ajaxOptions, thrownError) {
    alert(thrownError + 'xhr error -- ' + xhr.status);
}

});

在我的 mvc Controller 上,我有 JSON 对象作为字符串,因此我不需要 .NET JavascriptSerializer 和 JsonResult。

我的 JSON 字符串如下所示:

data2 = "{title:'1111111',start:'2014-03-23T16:00:00.000',end:'2014-03-23T18:00:00.000',id:107,hdtid:1,color:'#c732bd',allDay:false,description:''}"

我总是得到: “无效字符”

我已经尝试返回一个字符串并在客户端手动解析 JSON。因此我使用 ContentResult 作为返回类型但没有成功

    public class JsonStringResult : ContentResult
    {
        public JsonStringResult(string json)
        {
            Content = json;
            ContentType = "application/json";
        }
    }

这里有什么问题吗? JSON 看起来不错...

干杯, 斯特凡

最佳答案

试试吧 Json Controller

  public JsonResult fnname()
    {
        string variablename = "{title:'1111111',start:'2014-03-23T16:00:00.000',end:'2014-03-23T18:00:00.000',id:107,hdtid:1,color:'#c732bd',allDay:false,description:''}";
        return Json(variablename , JsonRequestBehavior.AllowGet);
    }

Jquery json 传递

 $(document).ready(function() {
   $.post("/controllername/fnname", { }, function (result) {
      alert(result);
   }, "json");
 });

关于jquery - 从 MVC Controller 返回 JSON 字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22528696/

相关文章:

javascript - 如何在MVC中的Jquery中显示二维数组?

javascript - 单击表格行以选择复选框并禁用/启用按钮 jquery

asp.net-mvc - 方法内的 RedirectToAction

c# - 在输出参数 CRM 中返回 EntityCollection 时出错

javascript - 如何将 CSS 类添加到此脚本函数

javascript - 没有类的元素上的jquery单击事件

asp.net - 获取 ASP.NET Core 2.0 中具有特定角色的所有用户

c# - 在 ASP.Net MVC 中保持干燥

java - 将 JsonNode 转换为 java 数组

JSON 到 Firebase