Andressa Pinheiro
Andressa Pinheiro

Reputation: 1637

"Uncaught TypeError: Illegal invocation" using POST request Ajax/NodeJs / MongoDB / Mongoose

I am getting an error in the console when I try to do a POST request thought Ajax/Jquery.

This is the error: enter image description here

The lines createTeam.js 67 and 31 are :

   $.ajax({   //line 67

   sendInvitation(teamID,_companyName,teamName) //line 31

the ajax request is inside the function sendInvitation. I call sendInvitation inside a success part of another ajax request:

 success: function(msg) {
                    $.cookie("teamID",msg.teamID)
                    $.cookie("sessionID",sessionID)
                    var teamID = msg.teamID;
                    sendInvitation(teamID,_companyName,teamName);
                    alert("team supposedly saved")
                }

This is sendInvitation function:

function sendInvitation(teamID, adminName,teamName){
        var emailList = [];
        $(".emails-ul").children("li").each(function(){
            emailList.push($(this).value);
        });
      $.ajax({
                type: "POST",
                url: "http://xxx.xxx.x.xx:9000/email/"+teamID,
                dataType:'json',
                contentType: false,
                data:{
                    teamName: teamName,
                    adminName:adminName, 
                    emails: "[email protected]"
                },
                success: function(msg) {

                }
          });
}

I dont know what I am doing wrong. Does someone know? Thank you in advance.

Upvotes: 0

Views: 793

Answers (2)

Andressa Pinheiro
Andressa Pinheiro

Reputation: 1637

I just found the solution.

I saw many questions about similar subject here answer.

Apparently, the Ajax request does not accept data as OBJECT.But I was not realizing that I was sending an object instead of a string. So in the calling of the function I was receiving an object in teamName (html object) and the Post was not working.

So once I fixed that to a string, the Post worked perfectly.

This can be very useful to people that has the same problem.

Check if you are trying to send an object instead of a string or json. The request will not work with objects.

contentType: false,

ContentType is not necessary to achieve this.

Cheers. :)

Upvotes: 0

Jessie A. Morris
Jessie A. Morris

Reputation: 2317

I think the issue is with your contentType object. It's likely that jQuery doesn't know what to do with a content type of false. Either don't set your contentType or set it to the correct value, likely either application/json or application/x-www-form-urlencoded.

Upvotes: 1

Related Questions