mr.cool mr.cool - 3 months ago 9
Ajax Question

How to POST two variable using ajax for save the data

I want to POST two variables. One is a JSON string and another one is a textbox value I am using C# for SQL server connection. Can anyone please check what is wrong in my code.

var myjsonstring = JSON.stringify(myjson);
var filename = $("#fname").val();

$.ajax({
type: "POST",
url: "http://localhost/name/Service/SaveData",
dataType: "json",
contentType: 'application/json',
data: JSON.stringify({
jsonobject: myjsonstring,filename
}),
success: function (data) {
alert('Success');

},
error: function () {
alert('Error');
}
});

Answer

You can pass an normal object to data of $.ajax:

data: {
    myjsonstring: myjsonstring,
    filename: filename
},
Comments