I am trying to post some data with jQuery Ajax, but the parameters in my Ajax method are null.
This is simple test to send data:
var dataPost = { ti
We don't need the contentType: 'application/json; charset=utf-8',
and $.toJSON
Here is the code makes me happy!
$(function () {
$("#btnSumbit").click(function () {
$('#results').hide();
$('#loadingmessage').show();
var a = $("#query").val();
$.ajax({
type: "POST",
url: "/Search/Index",
data: ({ query: a }),
datatype: "json",
success: function (data) {
$('#results').empty();
for (var i = 0; i < data.length; i++) {
var div = "<div>" + data[i].Name + "</div>";
$("#results").append(div);
}
$('#loadingmessage').hide();
$('#results').show();
},
failure: function (errMsg) {
$('#loadingmessage').hide();
alert(errMsg);
}
});
});
});
The Create
controller action doesn't expect parameters to be JSON serialized so you don't have to. Try passing them directly instead:
var dataPost = { titel: 'titel', message: 'msg', tagIds: 'hello' };
jQuery.ajax({
type: "POST",
url: "Create",
data: dataPost,
dataType: "json",
success: function(result) {
alert("Data Returned: ");
}
});