I have a variable in a javascript function which needs to be sent to the controller. This is the code in the javascript function.
var testString = "Test";
$.ajax({
type: "POST",
url: "@Url.Action("GetJavaScriptString")",
dataType: "json",
data: JSON.stringify(testString),
success: function (data) {
alert(data);
},
failure: function (errMsg) {
alert(errMsg);
}
});
And this is the controller method
public ActionResult GetJavaScriptString(string data)
{
return null;
}
The variable "data" in the GetJavaScriptString method remains null.
data: { data: testString },(notdata: JSON.stringify(testString),)