Having trouble passing data from JavaScript to ASP.cs using AJAX. The process doesn't seem to be working as expected. I am trying to pass a string with the results of a listbox but encountering issues. What could be causing this problem? Thank you.
public static bool AddNewItem(string name, string surname, int age)
{
return true;
}
<input type="submit" value="OK" id="enter" runat="server" onclick="submitAjax()" />
function submitAjax() {
var listBox = document.getElementById('SubCat');
var i;
var str=listBox.options[0].text;
for(i=1;i<listBox.options.length;i++)
str=","+listBox.options[i].text;
$.ajax({
type: 'POST',
url: 'RicercaAdvForn.aspx/AddNewItem',
data: '{str}',
contentType: "application/json; charset=utf-8",
dataType: "json",
success: function (msg) {
if (msg.d) {
alert("Successfully added new item");
}
},
error: function () {
alert("Error! Try again...");
}
} );
}