2014-04-04 128 views
2

我正在试图做一个MVC操作的JQuery .ajax后。这个Action然后应该返回一个JsonResult。目前,这个JsonResult是“”,但是我希望返回对象被Json填充。任何帮助?谢谢!JsonResult为JQuery返回null .ajax

$.ajax({ 
    type: 'post', 
    dataType: 'json', 
    url: url, 
    contentType: "application/json; charset=utf-8", 
    traditional: true, 
    data: JSON.stringify({ source: source }), 
    success: function (data) { 
debugger; 
    } 
}); 

public ActionResult PublishSource(string source) 
{ 
    return Json(new {data = "test"}); 
} 

编辑︰这是我现在的代码如下。 .ajax的成功方法仍为空返回数据。

$.ajax({ 
     type: 'post', 
     cache: false, 
     url: url, 
     contentType: "application/json", 
     success: function (data) { 
      debugger; 
     } 
    }); 


public JsonResult PublishSource() 
{ 
    var data = "test"; 
    return Json(data, JsonRequestBehavior.AllowGet); 
} 
+0

是进入成功的部分代码。这可能是一个错误,它正在跳过成功,并寻找显然不存在的错误函数。尝试将'public ActionResult PublishSource(string source)'更改为public'JsonResult PublishSource(string source).' content类型也是错误的并将其更改为'contentType:'application/json'' – CSharper

+0

成功方法我得到data = “” – jre247

+0

林不知道是什么问题,但也许它是因为你有传统的:真的?我使用JsonResults所有的代码,并且不设置它或内容类型。 – bsayegh

回答

0

改变你的控制器动作像如下

public JsonResult PublishSource(string source) 
{ 
    var data="test"; 
    return Json(data,JsonRequestBehaviour.AllowGet); 
} 
+0

我加了这个,但仍然没有运气。这是我目前的代码。现在成功的方法没有被击中。 $。AJAX({ 类型: '后', 数据类型: 'JSON', 网址:网址, 的contentType: “应用程序/ JSON”, 数据:JSON.stringify({源:源}), 成功:功能( data){ debugger; } }); – jre247

+0

和您更新的控制器操作? –

+0

是的,我也编辑了控制器动作。我已经把更新后的代码放在最上面 – jre247

0

试试这个,看看它是否工作:

return Json(new {dataObject = data}, JsonRequestBehavior.AllowGet); 

,然后成功,你得到这样的数据:

 success: function (data) { 
      debugger; 

var returnedData = data.dataObject; 
     } 
0

有相同或相似的问题w ith JSON结果在控制器返回之前确定,但在达到ajax成功回调时为NULL。

结果不是调用问题,而是模型上的访问修饰符被控制器转换为JSON结果。将其设置为public,结果在成功回调中不再为NULL。

希望这可以帮助任何人在相同的情况下尝试了其他答案。

0

使用JSON.NET作为默认串行序列化,而不是默认的Javascript串行JSON:

这将处理如果NULL发送数据的情况。

你需要在你的操作方法来写:

return Json(data, null, null); 

注:第二个和第三个参数为空在上面的功能是方便的Json方法的重载在Controller类。

下面是JsonNetResult类的代码。

public class JsonNetResult : JsonResult 
{ 
    public JsonSerializerSettings SerializerSettings { get; set; } 
    public Formatting Formatting { get; set; } 

    public JsonNetResult() 
    { 
     SerializerSettings = new JsonSerializerSettings(); 
     JsonRequestBehavior = JsonRequestBehavior.AllowGet; 
    } 

    public override void ExecuteResult(ControllerContext context) 
    { 
     if (context == null) 
      throw new ArgumentNullException("context"); 

     HttpResponseBase response = context.HttpContext.Response; 

     response.ContentType = !string.IsNullOrEmpty(ContentType) 
      ? ContentType 
      : "application/json"; 

     if (ContentEncoding != null) 
      response.ContentEncoding = ContentEncoding; 

     JsonTextWriter writer = new JsonTextWriter(response.Output) { Formatting = Formatting.Indented }; 

     JsonSerializer serializer = JsonSerializer.Create(SerializerSettings); 
     serializer.Serialize(writer, Data); 

     writer.Flush(); 
    } 
} 

你也需要添加以下代码BaseController如果任何在您的项目:

/// <summary> 
    /// Creates a NewtonSoft.Json.JsonNetResult object that serializes the specified object to JavaScript Object Notation(JSON). 
    /// </summary> 
    /// <param name="data"></param> 
    /// <param name="contentType"></param> 
    /// <param name="contentEncoding"></param> 
    /// <returns>The JSON result object that serializes the specified object to JSON format. The result object that is prepared by this method is written to the response by the ASP.NET MVC framework when the object is executed.</returns> 
    protected override JsonResult Json(object data, string contentType, System.Text.Encoding contentEncoding) 
    { 
     return new JsonNetResult 
     { 
      Data = data, 
      ContentType = contentType, 
      ContentEncoding = contentEncoding 
     }; 
    }