0

I have an AJAX call that based on its result - I send another call.

uploadDocument = function (doc1, doc2) {
    $.ajax({
        type: "POST",
        url: "/API/UploadDocs/addDocument",
        data: doc1,
        contentType: "application/json"
    }).then(function (result) {
        console.log(result);
        doc2.id=result;
        return $.ajax({
            type: "POST",
            url: "/API/UploadDocs/addDocument",
            data: doc2,
            contentType: "application/json"
        }).then(function (result) {
        });
    });
}

But I'm getting an Illegal invocation error, what am I doing wrong?

user3378165
  • 6,546
  • 17
  • 62
  • 101
  • Possible duplicate of [jQuery - Illegal invocation](https://stackoverflow.com/questions/10324594/jquery-illegal-invocation) – smarber Aug 24 '17 at 16:14
  • 1
    @smarber It is not a duplicate, I saw this question but didn't find an answer there to my issue, thank you. – user3378165 Aug 24 '17 at 19:50

2 Answers2

1

Illegal Invocation error arises when there is some error in the data being passed through AJAX

Check the type of doc1 and doc2.. Also try passing processData:false to the ajax.

rootkill
  • 599
  • 5
  • 10
1

You are doing promise chaining wrongly! When you return a promise you have to continue with the then that called the promise you are resolving.

Read the chaining section: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise/then

uploadDocument = function (doc1, doc2) {
    $.ajax({
        type: "POST",
        url: "/API/UploadDocs/addDocument",
        data: doc1,
        contentType: "application/json"
    }).then(function (result) {
        console.log(result);
        doc2.id=result;
        return $.ajax({
            type: "POST",
            url: "/API/UploadDocs/addDocument",
            data: doc2,
            contentType: "application/json"
        });
    }).then(function (result) {
      //Continue here
    });
}
Marco Talento
  • 2,335
  • 2
  • 19
  • 31