为什么这会引发内部服务器错误

时间:2013-05-02 04:04:37

标签: c# jquery json asp.net-mvc

我在控制器中有这个;

    public JsonResult Json_GetStoreList()
    {
        StoresData stores = new StoresData();
        return Json(stores.All());
    }

然后在我的部分视图中,我有这个;

$(function () {

    $.ajax({
        url: '/Maintenance/Json_GetStoreList',
        dataType: 'json',
        success: function (data) {
            alert(data);
        },
        error: function (xhr, ajaxOptions, thrownError) {
            alert(xhr.status);
            alert(thrownError);
        }
    });
});

控制器返回一个名为Store的对象的IEnumerable列表,如下所示;

public class Store
{
    public Guid id { get; set; }
    public int number { get; set; }
    public string name { get; set; }
}

Javascript抛出错误

  

500 - 内部服务器错误

2 个答案:

答案 0 :(得分:0)

尝试添加WebMethod属性

[WebMethod]     
public JsonResult Json_GetStoreList()

答案 1 :(得分:0)

试试这个

控制器

中的

[HttpPost]  
  public ActionResult Json_GetStoreList(MyViewModel myViewModel)  
  {            
    StoresData stores = new StoresData();
     return Json(stores , JsonRequestBehavior.DenyGet);
  }
Javascript

中的

 $.ajax({
         url: '/Maintenance/Json_GetStoreList',
         type: 'POST',
         dataType: 'json',
         contentType: 'application/json; charset=utf-8',
         async: true,
         processData: false,
         error: function (xhr) {
            alert('Error: ' + xhr.statusText);
         },
         success: function (result) {
            CheckIfInvoiceFound(result);
         },

      });
相关问题