Is it possible to put two URLs in an AJAX request?

424 views Asked by At

I have following code in my send.js:

 function send_upload_file(){
        var FD = new FormData();
       FD.append( $this.name, $this.value);
        $.ajax({
            url: 'upload',
            type: 'POST',
            processData: false,
            contentType: false,
            cache: false,
            data: FD,

            success: function (data) { 
            console.log('ok');
            },
            error: function () {
                alert("ERROR in upload");
            }
        });
    }

Can I put two links inside url:? (e.g url: 'upload, send')

2

There are 2 answers

0
Praveen Kumar Purushothaman On

No. If you wanna send two AJAX requests, you need to do it twice. But the shorthand for this would be:

var success = function () {
  console.log("OK");
};
$.post("upload", FD, success);
$.post("send", FD, success);

The above works asynchronously. If you want to do it synchronously, you need to do:

$.post("upload", FD, function () {
  console.log("OK");
  $.post("send", FD, function () {
    console.log("Sent");
  });
});
0
Sanooj T On

No you can't but try ajax after the first one success

$.ajax({
        url: 'upload',
        success: function (data) { 
               $.ajax({
                      url: 'send',
                   });
            },
            error: function () {
                alert("ERROR in upload");
            }
        });