JsonResult jQuery的阿贾克斯返回null [英] JsonResult returns null for Jquery .ajax

查看:79
本文介绍了JsonResult jQuery的阿贾克斯返回null的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图做一个jQuery阿贾克斯后,以一个MVC的行动。这个动作应该返回一个JsonResult。目前,这种JsonResult是,但是,我想返回的对象要填充JSON。任何帮助吗?谢谢!

  $。阿贾克斯({
  类型:'后',
  数据类型:JSON,
  网址:网址,
  的contentType:应用/ JSON的;字符集= UTF-8,
  传统:真实,
  数据:JSON.stringify({来源:来源})
  成功:功能(数据){
调试器;
  }
});公众的ActionResult PublishSource(字符串源)
{
    返回JSON(新{数据=测试});
}

编辑:这是我目前的code以下。仍然有在阿贾克斯是空的成功方法返回的数据。

  $。阿贾克斯({
        类型:'后',
        缓存:假的,
        网址:网址,
        的contentType:应用/ JSON
        成功:功能(数据){
            调试器;
        }
    });
公共JsonResult PublishSource()
{
    VAR数据=测试;
    返回JSON(数据,JsonRequestBehavior.AllowGet);
}


解决方案

更改您的控制器动作像下面

 公共JsonResult PublishSource(字符串源)
{
    VAR数据=测试;
    返回JSON(数据,JsonRequestBehaviour.AllowGet);
}

I am trying to do a Jquery .ajax post to an MVC action. This Action then should return a JsonResult. Currently, this JsonResult is "", however I want the return object to be filled with Json. Any help? Thanks!

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

Edit: Here's my current code below. Still having returned data in success method of .ajax being null.

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

解决方案

Change your controller action like below

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

这篇关于JsonResult jQuery的阿贾克斯返回null的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆