不能调用JSON结果jQuery中 [英] Cannot call the JSON result in Jquery
问题描述
我有一个返回结果为JSON结果控制器的操作。(我测试它准备好了,它这个动作效果很好)。
I have an action of the controller that return the result as the JSON result.(I test it ready, It works well with this action).
public JsonResult GetProductsByDepList(int id)
{
JsonResult jr = new JsonResult();
var _product = from a in DataContext.GetProductsByDep(id)
select new { ID = a.ID, ProName = a.Name};
jr.Data = _product.ToList();
jr.JsonRequestBehavior = JsonRequestBehavior.AllowGet;
return jr;
}
这就是我环路在我的观点:
And this is what I loop it in my view :
$(document).ready(function () {
var urlProduct = '<%: Url.Content("~/") %>' + "Products/GetProductsByDepList";
$.getJSON(urlProduct, function (dataPro) {
alert(123);
});
});
我只是警报测试它,但是当我载入我的网页不提醒什么。
谁能告诉我如何在jQuery中使用控制器的动作?
I just test it with alert, but it doesn't alert anything when I load my page. Could anyone tell me how to use Action of controller in jquery??
感谢,并欢迎所有的答案。
Thanks and welcome all your answers.
推荐答案
您可以用$阿贾克斯jQuery的,如果你的JSON格式像这样
you can use with $.ajax in jquery if your json format like this
{行:[{ID:1,用户名:富},{ID:2,用户名:酒吧} ]}
$.ajax({
url: urlProduct,
type:"GET",
success:function (data) {
$.each(data.rows,function(i,rows){
alert(rows.username);//will be show username foo and bar
});
}
});
这是参考 http://api.jquery.com/jQuery.ajax/
这是参考JSON http://www.json.org/
这是库可以使用C# http://sourceforge.net/projects/csjson/
这篇关于不能调用JSON结果jQuery中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!