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?

2
  • Possible duplicate of jQuery - Illegal invocation Commented Aug 24, 2017 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. Commented Aug 24, 2017 at 19:50

2 Answers 2

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
    });
}
Sign up to request clarification or add additional context in comments.

Comments

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.

2 Comments

Thank you, when I called the uploadDocument function I sent an incorrect parameter, thanks!
Glad I could help

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.