开发者

get json object with jquery

开发者 https://www.devze.com 2023-03-23 10:41 出处:网络
$.getJSON(\"<%: Url.Action(\"myUrl\", \"cont\") %>/\", function(data) { var items = []; $.each(data, function(key, val) {
$.getJSON("<%: Url.Action("myUrl", "cont") %>/", function(data) {
        var items = [];
        $.each(data, function(key, val) {
            items.push(val);
        });
     });

    [Authorize]
    [OutputCache(Duration = 0, VaryByParam =开发者_C百科 "None")]
    public JsonResult myUrl()
    {
        var list = _repository.GetAll();
        var items = list.Select(c => c.Name).ToList();

        return Json(items, JsonRequestBehavior.AllowGet);
    }

I create a list on the server side (list of string names) and return a JsonResult. I'm trying to get the list on the client side using jquery so i can check if it contains a particular item. The above doesnt seem to work...any suggestions?


You have to parse the JSON:

$.get("<%: Url.Action("myUrl", "cont") %>/", function(data) {
    var items = [];
    data = $.parseJSON(data);
    $.each(data, function(key, val) {
        items.push(val);
    });
 });
0

精彩评论

暂无评论...
验证码 换一张
取 消