get json object with jquery

≯℡__Kan透↙ 提交于 2019-12-12 04:08:15

问题


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

    [Authorize]
    [OutputCache(Duration = 0, VaryByParam = "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?


回答1:


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);
    });
 });


来源:https://stackoverflow.com/questions/6827172/get-json-object-with-jquery

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!