Dev Studio
Dev Studio

Reputation: 13

Simultaneous AJAX calls throwing an error

I copied this from a reliable source and altered it for my program

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

Error msg:

(1)An error just happened...message: Unexpected Token

(2)An error just happened...message: Unexpected Token

Upvotes: 1

Views: 54

Answers (1)

Haresh Vidja
Haresh Vidja

Reputation: 8496

I think you have to use simultaneous AJAX request in this way.

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

Upvotes: 1

Related Questions