同时 AJAX 调用引发错误

Simultaneous AJAX calls throwing an error

我从可靠的来源复制了它并为我的程序修改了它

           var data1 = 'name=Joe';
           var data2 = 'name=Jill';
           $.when(
                $.ajax({
                        url: '/a/sso',
                        headers: {
                            'Content-Type': 'application/x-www-form-urlencoded'
                        },
                        type: "POST",
                        dataType : "json",
                        data : data1,
                        error: function(err1) {
                            console.log('(1)An error just happened...' + err1);
                        }
                    }),
                $.ajax({
                    url: '/a/sso',
                    headers: {
                        'Content-Type': 'application/x-www-form-urlencoded'
                    },
                    type: "POST",
                    dataType : "json",
                    data : data2,
                    error: function(err2) {
                        console.log('(2)An error just happened...' + err2);
                    }
                })
              ).then(function( data1, data2 ) {
                   console.log('ok')
              })

错误信息:

(1)刚刚发生错误...消息:意外令牌

(2)刚刚发生错误...消息:意外令牌

我认为你必须以这种方式使用同步 AJAX 请求。

var data1 = 'name=Joe';
var data2 = 'name=Jill';

var async_request=[];
// you can push  any aysnc method handler
async_request.push($.ajax({
    url: '/a/sso',
    headers: {
        'Content-Type': 'application/x-www-form-urlencoded'
    },
    type: "POST",
    dataType : "json",
    data : data1,
    error: function(err1) {
        console.log('(1)An error just happened...' + err1);
    }
}));

async_request.push($.ajax({
    url: '/a/sso',
    headers: {
        'Content-Type': 'application/x-www-form-urlencoded'
    },
    type: "POST",
    dataType : "json",
    data : data2,
    error: function(err2) {
        console.log('(2)An error just happened...' + err2);
    }
}));


$.when.apply(null, async_request).done( function(){
    // all done
    console.log('all request completed')
    console.log(responses);
});