Ajax获取方法错误

时间:2014-07-04 10:49:49

标签: javascript ajax asp.net-mvc

我有这个javascript代码,它必须获取我的viewmodel列表,但是不调用success函数,调用error函数。 我的错误是什么?

var id = 5;
    var request = $.ajax({
        url: "/ArizaTalep/Get_List?tid=" + id,
        type: "POST",
        dataType: "json",
        data: "{}",
        contentType: 'application/json; charset=utf-8',
        success: function (data) {  },
        error: function (data) { alert("error!!") }

    });

控制器:

    public List<DURUM_HAREKET_ViewModel> Get_List(int tid)
    {
        DH_DataModel dmodel = new DH_DataModel();
        var ll = dmodel.GetAll().Where(i => i.T_ID == tid).ToList();
        return ll;
    }

3 个答案:

答案 0 :(得分:1)

尝试将您的操作修改为:

[AcceptVerbs(HttpVerbs.Post)]
public ActionResult Get_List(int tid)
{
   DH_DataModel dmodel = new DH_DataModel();
   var ll = dmodel.GetAll().Where(i => i.T_ID == tid).ToList();
   return Json(ll);
}

var request = $.ajax({
        url: "/ArizaTalep/Get_List",
        type: "POST",
        dataType: "json",
        data: {tid: id},
        contentType: 'application/json; charset=utf-8',
        success: function (data) {  },
        error: function (data) { alert("error!!") }

    });

答案 1 :(得分:1)

将控制器更改为:

public ActionResult Get_List(int tid)
{
    DH_DataModel dmodel = new DH_DataModel();
    var ll = dmodel.GetAll().Where(i => i.T_ID == tid).ToList();
    return Json(ll, JsonRequestBehavior.AllowGet);
}

<强>说明: 您需要返回Json类型才能将其置于View Success函数中。

答案 2 :(得分:1)

public ActionResult Get_List(int tid)
{
  DH_DataModel dmodel = new DH_DataModel();
  var ll = dmodel.GetAll().Where(i => i.T_ID == tid).ToList();
  return Json(ll, JsonRequestBehavior.AllowGet);
}