两个 JSON 请求的列表

List of two JSON requests

本文关键字:列表 请求 JSON 两个      更新时间:2023-09-26

我不明白如何做正确的事情。通过此查询,我得到了一个带有文件名的 JSON:

$.getJSON("http://api.server.com/my/?callback=?",
    function(data){
        var results = [];
        $.each(data['results'], function(i, result) {
            results.push("<div class='accordion-group span4'><div class='accordion-heading'>Video Frame<blockquote><a href='http://server.com/video/?my=" + result.File + "' target='_blank'><img src='http://jpg.server.com/" + result.File + ".jpg' class='img-polaroid'/></a><p>" + result.ListId + "</p><small>" + result.OwnerId + "</small><small>" + result.updatedAt + "</small>    </blockquote><a class='accordion-toggle' data-toggle='collapse' data-parent='#accordion2' href='#" + result.File + "'>Share video</a></div><div id='" + result.File + "' class='accordion-body collapse'><div class='accordion-inner'><form class='share_file_form'>Set list<input name='nd' id='user_id' type='hidden'><input name='file' value = '" + result.File + "' type='hidden'><div class='list'></div><input type='text' placeholder='New list'><div class='modal-footer'><button class='share_file_submit'>Share video</button></div></form><div id='user_info_result'></div></div></div></div>");
        });
        $('#mytile').html(results.join(""));
    }
);

通过此查询,我得到了一个带有标签名称的 JSON:

$.getJSON("http://api.server.com/list/?callback=?",
    function(data){
    var results = [];
    $.each(data['results'], function(i, result) {
        results.push("<label class='radio'><input type='radio' name='list' id='" + result.List + "' value='" + result.List + "' checked>" + result.List + "</label>");
    });
    $('.my_list').html(results.join(""));
    }
);

最后,我需要显示文件列表。每个文件旁边应显示一个带有文件名和标签列表的表单:

$(document).on('click', '.share_file_form', function() {
    $('.share_file_form').validate({
        submitHandler: function(form) {
            $.ajax({
                type: "GET",
                url: "http://api.server.com/set/",
                timeout: 20000,
                data: $(form).serialize(),
                beforeSend: function() {
                    $(".share_file_submit").attr("disabled", true);
                    $(".share_file_submit").html("Send <img src='http://src.server.com/loadr.gif' border='0'/>");
                },
                success: function(msg){
                    console.log("Data Saved: " + msg);
                    $("#share_file_submit").attr('disabled', false);
                    $("#share_file_submit").html("Share video");
                    $("#user_info_result_2").html(msg);
                },
                error: function(msg){
////////////////            $('#user_info_result_2').html("<div class='alert alert-error span3'>Failed from timeout. Please try again later. <span id='timer'></span> sec.</div>");
                }
            });
        }
    });

});

所有这些都有效。

问题是如何确保每个表单将单独工作?现在只适用于第一种形式。如果您按下其他表单上的按钮,第一个表单仍然有效。

您的错误在于您识别用户单击的表单的方式。以下行是问题所在:

$('.share_file_form').validate({

无论您单击的内容如何,您始终会使用此选择器获得第一个表单。

以下是您需要的:

$(document).on('click', '.share_file_form', function(event) {
    $(event.target).validate({
        submitHandler: function(form) {

嗯。我"认为"我知道你想要什么。

如何将第二个调用包装在一个方法中,或者两者兼而有之,ala。

 function fireJSON(){
    var func1 = function(){
      //your first json code
      func2(); // call your second json code
    }
    var func2 = function(){
          // second json code
    }
     return {
       func1: func1
     }
   }

   fireJSON().func1();