I want to call the server method through the AJAX call. But when I am click the button and call the AJAX function at that time it shows an error.
Here is my code
<input type="button" id="btn_findsubmit" value="Edit" class="button" />
$(document).on("click", "#btn_findsubmit", function (e) {
var c = $find("<%=cmbobx_search.ClientID %>");
$.ajax({
type: "POST",
url: "schoolregistration.aspx/GetSchoolName",
data: json.stringify({ schoolname: c.get_textboxcontrol().value }),
contenttype: "application/json; charset=utf-8",
datatype: "json",
success: OnSuccessGetSchoolName,
failure: function () {
alert("error! try again...");
}
});
});
[WebMethod] [ScriptMethod]
public static string GetSchoolName(string schoolName){
//Here is the code
}
Now when I click the button at that time JavaScript button click event is working but the ajax method do not calling the server method GetSchoolName (I know by doing debug mode).
And throws a error that:
ReferenceError: json is not defined
I want to call the server method through the AJAX call. But when I am click the button and call the AJAX function at that time it shows an error.
Here is my code
<input type="button" id="btn_findsubmit" value="Edit" class="button" />
$(document).on("click", "#btn_findsubmit", function (e) {
var c = $find("<%=cmbobx_search.ClientID %>");
$.ajax({
type: "POST",
url: "schoolregistration.aspx/GetSchoolName",
data: json.stringify({ schoolname: c.get_textboxcontrol().value }),
contenttype: "application/json; charset=utf-8",
datatype: "json",
success: OnSuccessGetSchoolName,
failure: function () {
alert("error! try again...");
}
});
});
[WebMethod] [ScriptMethod]
public static string GetSchoolName(string schoolName){
//Here is the code
}
Now when I click the button at that time JavaScript button click event is working but the ajax method do not calling the server method GetSchoolName (I know by doing debug mode).
And throws a error that:
ReferenceError: json is not defined
json.stringify
need to be JSON.stringify
– Death-is-the-real-truth
Commented
Jul 8, 2017 at 5:37
JSON.stringify()
capital letters
– Wesley Smith
Commented
Jul 8, 2017 at 5:37
It should be JSON.stringify
, not json.stringify
<input type="button" id="btn_findsubmit" value="Edit" class="button" />
<script>
$(document).on("click", "#btn_findsubmit", function (e) {
$.ajax({
type: "POST",
url: "Default.aspx/GetSchoolName",
data: JSON.stringify({ schoolName: "school name" }),
contentType: "application/json; charset=utf-8",
datatype: "json",
success: function (data) {
alert(data.d);
},
failure: function () {
alert("error! try again...");
}
});
});
</script>
[WebMethod]
public static string GetSchoolName(string schoolName)
{
//Here is the code
return "success";
}
first of all it must be JSON.stringify
not json.stringify
, second it must be contentType
not contenttype
third the name of parameters in [WebMethod]
must be same as in your ajax data.
in this case schoolName
not schoolname
.
hope it help you.