Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/86.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/grails/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Jquery 循环从控制器返回的json数据_Jquery_Asp.net Mvc 3_C# 4.0 - Fatal编程技术网

Jquery 循环从控制器返回的json数据

Jquery 循环从控制器返回的json数据,jquery,asp.net-mvc-3,c#-4.0,Jquery,Asp.net Mvc 3,C# 4.0,我需要遍历从控制器返回的json对象 我的控制器代码 public ActionResult GetJobs() { var jobs = new JobConfig().getJobs(User.Identity.Name); return Json(jobs.ToList(),JsonRequestBehavior.AllowGet); } 我的javascript代码 $(document).ready(function ()

我需要遍历从控制器返回的json对象

我的控制器代码

    public ActionResult GetJobs()
    {
        var jobs = new JobConfig().getJobs(User.Identity.Name);

        return Json(jobs.ToList(),JsonRequestBehavior.AllowGet);

    }
我的javascript代码

$(document).ready(function () {


    $('#search').click(function () {


        $.when(
            getUser(), //Get User Details
            getJobs()//Get Jobs
          ).then(process);

    });

});

function getUser() {

    return $.ajax({
        type: 'GET',
        url: '/ManageUsers/GetUser',
        data: { UserName: $('#txtUser').val() },
        dataType: 'json'
    });
}

function getJobs() {

      return $.ajax({
        type: 'GET',
        url: '/ManageUsers/GetJobs',
        dataType: 'json'
    });
}

function process(user,jobs) {

    //Show the user here

    if (user[0].Name != null) {


        var list = '<p> User : ' + user[0].Name + '<br/>';
        list += 'Email : ' + user[0].Email + '<br/>';
        list += 'Role : ' + user[0].Role + '<br/>';
        list += 'UserData : ' + user[0].UserData;
        list += '</p><br/>';


        alert(jobs.length);

        $('#user').empty();
        $('#user').append(list);



    }
    else {
        $('#user').empty();
        $('#user').append('User not exists');
    }
}
但我只关心0元素

我认为这就是使用return$.ajax函数返回结果的方式

关于从jobs数组中获取第一个元素有什么想法吗

问候,

南非航空公司

给定一个JSON数据对象,根据您的示例,该对象已从您的操作结果返回,您可以使用以下命令访问第一个元素:

someReturnedData[0]
也就是说,如果您想要“JobId”,您可以通过以下方式访问它:

someReturnedData[0].JobId
编辑:

根据您的评论,您可以修改GetJobs函数以包含一个成功处理程序。执行所需的任何处理,然后将该对象返回到流程函数。也许是这样的:

function getJobs() {

    var jobs;

    $.ajax({
        type: 'GET',
        url: '/ManageUsers/GetJobs',
        dataType: 'json',
        success: function(data) {
           jobs = data[0];
        }
    });

    return jobs;
}
给定一个JSON数据对象,根据您的示例,该对象已从您的操作结果返回,您可以使用以下命令访问第一个元素:

someReturnedData[0]
也就是说,如果您想要“JobId”,您可以通过以下方式访问它:

someReturnedData[0].JobId
编辑:

根据您的评论,您可以修改GetJobs函数以包含一个成功处理程序。执行所需的任何处理,然后将该对象返回到流程函数。也许是这样的:

function getJobs() {

    var jobs;

    $.ajax({
        type: 'GET',
        url: '/ManageUsers/GetJobs',
        dataType: 'json',
        success: function(data) {
           jobs = data[0];
        }
    });

    return jobs;
}

您可以包含从您的操作结果返回的json字符串的示例吗?@Jesse这是我得到的[{“JobId”:“ArtListLoad”,“JobDesc”:“GOLD Article List Load”,“JobFilePrefix”:null,“JobFileRCDir”:null,“JobFileDataDir”:null,“JobFileExtension”:null,“JobFileDataRange”:null,“JobScript”:null}]“您可以包含从您的操作结果返回的json字符串的示例吗?@Jesse这是我得到的[{“JobId”:“ArtListLoad”,“JobDesc”:“GOLD Article List Load”,“JobFilePrefix”:null,“JobFileRCDir”:null,“JobFileExtension”:null,“JobFileDataRange”:null,“JobScript”:null}]"我正在从json对象的responseText属性获取json数据。我将如何将responseText属性提取到变量中?如果您有任何示例,感谢您的回复。我现在已经离开了工作,所以现在无法测试它。我将在明天早上第一件事检查它并让您知道。我按照您在get jobs方法中的建议使用了它,但它没有工作。但当我在process method中使用它时,它工作了。感谢您的帮助。我正在从json对象的responseText属性获取json数据。我将如何将responseText属性提取到var中?如果您有任何示例,感谢您的回复。我现在已经离开了工作,所以现在无法测试它。明天早上第一件事就是检查它,然后让我们继续你知道。我按照你在“获取工作”方法中的建议使用了它,但它不起作用。但当我在“过程”方法中使用它时,它起了作用。谢谢你的帮助