Is this valid data for jQuery ajax to call aspnet mvc controller?
I am using jquery with asp.net mvc .... Is my data parameter valid or am I missing something ...
$.ajax({
type:"POST",
url: "Materials/GetRecords",
data: "{'currentPage':1,'pageSize':5}",
Any suggestion....
EDIT: I am calling a function on the view page,
<asp:Content ID="Content2" ContentPlaceHolderID="MainContent" runat="server">
<script type="text/javascript">
$(document).ready(function() {
getMaterials(0);
});
</script>
and my function,
function getMaterials(currentPage) {
$.ajax({
type:"POST",
url: "Materials/GetRecords",
data: "{'currentPage':" + (currentPage + 1) + ",'pageSize':5}",
contentType: "application/json; charset=utf-8",
cache: false,
global: false,
async: false,
dataType: "json",
success: function(data) {
var divs = '';
$("#ResultsDiv").empty();
$.each(data.Results, function() {
divs += '<div class="resultsdiv"><br /><span style="display: inline-block;width:150px;" class="resultName">' + this.Mat_Name + '</span><span class="resultfields" style="padding-left:10px;">Measurement :</span> <span class="resultfieldvalues">' + this.Mes_Name + '</span> <a href="/Materials/Delete/' + this.Id + '">Delete</a> <a href="/Materials/Details/' + this.Id + '">Details</a> <a href="/Materials/Edit/' + this.Id + '">Edit</a></div>';
});
alert("hai");
$("#ResultsDiv").append(divs);
$(".resultsdiv:even").addClass("resultseven");
$(".resultsdiv").hover(function() {
$(this).addClass("resultshover");
}, function() {
$(this).removeClass("resultshover");
});
$("#HfId").val("");
$("#HfId").val(data.Count);
}
});
return false;
}
on loading the view page I see a warning [Object object]
.. Why is this ...
+2
a source to share
2 answers
Your data is now a string. You probably want it to be an object like below:
$.ajax({
type:"POST",
url: "Materials/GetRecords",
data: {"currentPage": (currentPage + 1), "pageSize": 5},
contentType: "application/json; charset=utf-8",
cache: false,
global: false,
async: false,
dataType: "json",
success: function(data) {
var divs = '';
$("#ResultsDiv").empty();
$.each(data.Results, function() {
divs += '<div class="resultsdiv"><br /><span style="display: inline-block;width:150px;" class="resultName">' + this.Mat_Name + '</span><span class="resultfields" style="padding-left:10px;">Measurement :</span> <span class="resultfieldvalues">' + this.Mes_Name + '</span> <a href="/Materials/Delete/' + this.Id + '">Delete</a> <a href="/Materials/Details/' + this.Id + '">Details</a> <a href="/Materials/Edit/' + this.Id + '">Edit</a></div>';
});
alert("hai");
$("#ResultsDiv").append(divs);
$(".resultsdiv:even").addClass("resultseven");
$(".resultsdiv").hover(function() {
$(this).addClass("resultshover");
}, function() {
$(this).removeClass("resultshover");
});
$("#HfId").val("");
$("#HfId").val(data.Count);
}
});
It's okay for you to return a JavaScript object ([Object object]). What you do with this depends on your application. However, you are right there with no warning (other than hi), so I don't know where it comes from. Try clearing your browser cache.
+1
a source to share