JQuery data method

In my code, I want to send two parameters to my data. Name

is one of my parameter names and its value is in a variable a

. Another parameter name type

with a value in a variable str

. Didn't work for me:

 $.ajax({
  type: "POST",
  url: "./server",
  data: "name="+a+"type="+str,

      success: function(msg){
        alert( "Data Saved: " + msg);
    }
})

      

Any suggestions?

0


a source to share


2 answers


This is how I do it:

$.ajax({
  type: "POST",
  url: "./server",
  data: "name="+a+"&type="+str,

      success: function(msg){
        alert( "Data Saved: " + msg);
    }
})

      



Just like querystrings. Don't forget to include the correct URL. Like PHP urlencode () , only, in JavaScript (look at escape (), however this is not a complete implementation either).

+1


a source


Try something like this:

 $.ajax({
  type: "POST",
  url: "./server",
  data: {name:a, type: str},

      success: function(msg){
        alert( "Data Saved: " + msg);
    }
})

      



with literals instead of variables it would be like this:

 $.ajax({
  type: "POST",
  url: "./server",
  data: {name: "some name", type: "some type"},

      success: function(msg){
        alert( "Data Saved: " + msg);
    }
})

      

+4


a source







All Articles