Yajat Singh
Yajat Singh

Reputation: 43

JQuery Ajax gives error on sending multiple data

name = "name1";
uname = "username";
pass = "password";
$.ajax({
  type: 'POST',
  url: "url",
  data: {
    name: name,
    username: uname,
    password: pass
  },
  success: function(data, textStatus, jqXHR) {
    alert(data);
  },
  error: function(jqXHR, textStatus, errorThrown) {
    alert("Error");
  }


});

This code gives an error while running :

Uncaught TypeError: Illegal invocation

But if I change the number of parameters at data to 1, it runs fine. I tried to specify datatype, but to no avail

Upvotes: 0

Views: 503

Answers (2)

Erick Boshoff
Erick Boshoff

Reputation: 1583

have you tried JSON.stringify? See the following code example:

JAVASRIPT

name="name1";
uname = "username";
pass = "password";

var data = { name:name , username:uname , password: pass}; 

$.ajax({
    type: 'POST',
    url: "url",
    data: JSON.stringify(data),
    success: function (data, textStatus, jqXHR) {
      alert(data); 
    },
    error: function (jqXHR, textStatus, errorThrown) {
        alert("Error");
    }
});

w3schools

A common use of JSON is to exchange data to/from a web server.

When sending data to a web server, the data has to be a string.

Convert a JavaScript object into a string with JSON.stringify().

Upvotes: 0

nverm
nverm

Reputation: 26

I think your data is array and not text. Try checking it again, I ran into a same issue some time ago.

Upvotes: 1

Related Questions