tags:

views:

51

answers:

2

In my code I want to send two parameters in my data. Name is one of my parameter names and its value is in a variable a. Another parameter name is type with the value in the str variable. The following did not work for me:

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

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

Any suggestions?

+4  A: 

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);
    }
})
Steve Willcock
i want to insert the variable in that data data: {name:a, type: str},where a and str are my variable ..But its not working..Suggest me
Aruna
The data is not a string it's a javascript / json object. So you don't need to concatenate anything - just type it as shown with the {} brackets and the : instead of = and it should work ok.
Steve Willcock
+1  A: 

Here's how I do it:

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

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

Just like querystrings. Remember to proper URL Encode them though. Like PHP's urlencode(), only, in JavaScript (look at escape(), however that one is not a full implementation either).

kastermester