Ajax调用控制器方法不传递参数

时间:2012-11-08 14:42:29

标签: asp.net-mvc jquery

我试图对控制器方法进行AJax调用,无论我尝试什么,参数都为null。我已经关注了所有类似的SO帖子,但无济于事。对不起,如果有答案,我找不到。我的代码是......

Ajax Call

    var sguid = $(nTr).attr('id');
    $.ajax({
    url: "/Dashboard/Reporting/GetBlacklistedSoftwareItems",
    type: 'POST',
    dataType: 'json',
    data: JSON.stringify({guid: sguid}),
    statusCode: {
        404: function() {
    alert("page not found");
        }
    },
    success: function (data) {
    //DO Something
    },
    error: function () {
        alert("error");
    }
});

控制器方法

public JsonResult GetBlacklistedSoftwareItems(string guid)
{
    List<DeviceSoftware> ldevice = new List<DeviceSoftware>();
    Guid i = Guid.Parse(guid);
    ReportMethods reportingMethods = new ReportMethods();
    ldevice = reportingMethods.GetNonCompliantApplicationReport(CompanyId);

    DeviceSoftware ds = ldevice.Find(x => x.Device.Guid == i);
    List<DeviceApplication> da = new List<DeviceApplication>();
    if (ds != null)
    {
        da = ds.DeviceApplications;
    }

    return Json(da, JsonRequestBehavior.AllowGet);
}

该方法正在被guid点击nullsguid确实保存了我想要传递的数据。 有人能告诉我我错过了什么吗?

3 个答案:

答案 0 :(得分:1)

反对我读过的所有内容我改变了

data: JSON.stringify({guid: sguid}),

data: {guid: sguid},

现在正在工作。

答案 1 :(得分:0)

佛瑞德,

您需要将GetBlacklistedSoftwareItems设为post方法....

试试这个......

[HttpPost] public JsonResult GetBlacklistedSoftwareItems(string guid) {

答案 2 :(得分:0)

需要进行小的改动。

var sguid = $(nTr).attr('id');
    $.ajax({
    url: "/Dashboard/Reporting/GetBlacklistedSoftwareItems",
    contentType: "application/json; charset=utf-8" ,//This is very important 
    type: 'POST',
    dataType: 'json',
    Data: JSON. stringify ({guild: squid}),
    statusCode: {
        404: function() {
    alert("page not found");
        }
    },
    success: function (data) {
    //DO Something
    },
error: function () {
    alert("error");
}

});

contentType:“application / json; charset = utf-8”, 添加到$ .Ajax调用中。 :)

相关问题