jQuery 从 MVC 控制器返回 JSON 字符串

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/22528696/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-27 01:57:58  来源:igfitidea点击:

return JSON string from MVC controller

jqueryasp.net-mvcjsonasp.net-mvc-4

提问by stefan

I use the following code to send/receive an object to my mvc controller:

我使用以下代码向我的 mvc 控制器发送/接收对象:

$.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);
}

});

});

On my mvc controller I have my JSON object as string so I don't need the .NET JavascriptSerializer and JsonResult.

在我的 mvc 控制器上,我有我的 JSON 对象作为字符串,所以我不需要 .NET JavascriptSerializer 和 JsonResult。

My JSON string looks like:

我的 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:''}"

And I always get: "Invalid character"

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

I already tried to return a string and parse the JSON manually on client side. Therefore I used ContentResult as return type but with no success

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

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

What is the problem here? The JSON looks fine...

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

Cheers, Stefan

干杯,斯蒂芬

回答by Dinesh

Just Try it Json Conrtroller

试试吧 Json 控制器

  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 passing

jquery json 传递

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

回答by huocp

Your data2 is INVALIDJSON string. It should be:

您的 data2 是无效的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\":\"\"}"

Read JSON standard here http://json.org

在这里阅读 JSON 标准http://json.org

JSON is more strict than plain javascript, key has to be wrapped in double quote, and string has to be wrapped in double quote too, single quote is invalid.

JSON 比普通的 javascript 更严格,key 必须用双引号括起来,字符串也必须用双引号括起来,单引号无效。

Douglas Crockford designed strict format of JSON for reasons. http://www.yuiblog.com/blog/2009/08/11/video-crockford-json/

Douglas Crockford 出于某些原因设计了严格的 JSON 格式。http://www.yuiblog.com/blog/2009/08/11/video-crockford-json/

His home page has many valuable links too. http://javascript.crockford.com

他的主页也有许多有价值的链接。http://javascript.crockford.com