Kok*_*ila 4 javascript c# asp.net-mvc jquery
我从Jquery ajax调用MVC控制器方法.
$(document).ready(function () {
$.ajax({
type: "POST",
url: "/Customer/GetDetails",
contentType: "application/json; charset=utf-8",
async: false,
cache: false,
success: function (data) {
$.each(data, function (index, value) {
alert(value);
});
}
});
});
Run Code Online (Sandbox Code Playgroud)
我有一个名为Customer的实体.
Controller Method从DB中获取记录并存储为Customer of List,并以JsonResult类型返回该列表.
public JsonResult GetDetails()
{
CustomerDAL customer = new CustomerDAL();
IList<Customer> custList = customer.GetDetail();
return Json(custList);
}
Run Code Online (Sandbox Code Playgroud)
但是我的ajax的成功功能根本就不在这里.
您不必指定content-type,因为它设置在该服务器所期望的数据类型,你是不发送任何所以无需设置明确,其次设置dataType到json该格式在客户端所期待的数据从服务器.但我真的怀疑它可能是任何错误的原因.
添加错误回调以确保在回来的路上出现问题
尝试
$.ajax({
type: "POST",
url: "/Customer/GetDetails",
dataType:'json',
async: false,//WHY??
cache: false,
success: function (data) {
alert("success");
},
error:function(){
alert("something went wrong");
}
});
Run Code Online (Sandbox Code Playgroud)
使用
Firebug或chrome工具用于检查ajax请求并设置返回的状态代码.
在Controller内部放置一个断点,以查看是否ActionResult在进行调用时命中了该断点.
编辑
用HttpPost注释标记你的JsonResult
[HttpPost]
public JsonResult GetDetails()
{
CustomerDAL customer = new CustomerDAL();
IList<Customer> custList = customer.GetDetail();
return Json(custList);
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
15842 次 |
| 最近记录: |