Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/70.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
从两个AJAX请求构造JavaScript对象_Javascript_Jquery_Ajax_Json_Object - Fatal编程技术网

从两个AJAX请求构造JavaScript对象

从两个AJAX请求构造JavaScript对象,javascript,jquery,ajax,json,object,Javascript,Jquery,Ajax,Json,Object,我有一个API,用于获取表示船只的对象数组,它为每艘船只返回“id”、“name”、“length”和“harbour_id” 我还有另一个API,用于从“harbour\u id”获取“harbour\u name”。虽然为两者编写AJAX请求不是问题,但让我困惑的是:我必须构造一个对象数组,其中每艘船都包含其EF的所有数据,包括“harbour_name” 我该怎么做 让我们编写一些代码: var boats; function getBoats() { $.ajax({ u

我有一个API,用于获取表示船只的对象数组,它为每艘船只返回“id”、“name”、“length”和“harbour_id”

我还有另一个API,用于从“harbour\u id”获取“harbour\u name”。虽然为两者编写AJAX请求不是问题,但让我困惑的是:我必须构造一个对象数组,其中每艘船都包含其EF的所有数据,包括“harbour_name”

我该怎么做

让我们编写一些代码:

var boats;
function getBoats() {
    $.ajax({
    url:'/',
    type:'GET',
    success: gotBoats,
    error: didntGetBoats,
    });
}

function gotBoats(data, textStatus, jqXHR) {
    boats = $.parseJSON(data);
        // I presume I need to make another request somewhere around here
        // but I'm not sure how to do it to bind the response data to
        // the correct boat
}

function didntGetBoats(jqXHR, textStatus, errorThrown) {
    alert(errorThrown);
}
在这个问题解决后,我还需要能够反向操作——输入一艘新船,将其位置发布到适当的位置,并将其余数据发布到适当的位置


提前感谢您提供的提示:)

您不必再提出一个请求,您必须为每艘船提出一个请求,这将非常低效。如果有任何方法可以将港口名称包含在第一个响应中,那就是您想要做的

但是如果你不能,你可以发出一系列关于船只的请求:

function gotBoats(data, textStatus, jqXHR) {
    // Somethign to keep track of how many harbour name responses we've seen
    var harbourNameCount = 0;

    // Get the boats. (Do you need that parseJSON call? jQuery will
    // do it automatically if the Content-Type of the response is correct;
    // or you can add `dataType: 'json'` to the `ajax` call.)
    boats = $.parseJSON(data);

    // Loop through requesting harbour names
    $.each(boats, function(i, boat) {
        // Ask for this harbour name, using ES5's Function#bind to
        // pass the relevant boat into the callbacks. You'll need
        // a shim for Function#bind on IE8 and earlier.
        $.ajax({
            url:      "/path/to/harbour/name/query",
            data:     {harbour_id: boat.harbour_id},
            success:  gotHarbour.bind(null, boat),
            error:    didntGetHarbour.bind(null, boat),
            complete: finishHarbour
        });
    });

    // Callback for when we get the name; note that since we use this
    // with bind, the boat is our first argument.
    function gotHarbour(boat, data) {
        boat.harbour_name = data.harbour_name;
    }

    // Callback for when we don't get the name for some reason
    function didntGetHarbour(boat) {
        boat.harbour_name = "(Unknown: " + boat.harbour_id + ")";
    }

    // Called after the two callbacks above
    function finishHarbour() {
        // Count this response
        ++harbourNameCount;

        // If we have seen all the responses, we're done!
        if (harbourNameCount === boats.length) {
            allFinishedLoading();
        }
    }
}

卓越的是,这正是我所缺少的:)我知道我需要执行多个请求,恐怕API是原样的:-/但是,非常感谢,现在我可以回到正轨了。