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:
Share Improve this question edited Oct 17, 2017 at 17:32 halfer 20.4k19 gold badges109 silver badges202 bronze badges asked Jul 8, 2017 at 5:35 s.k.Sonis.k.Soni 1,3502 gold badges22 silver badges45 bronze badges 3ReferenceError: json is not defined
-
json.stringify
need to beJSON.stringify
– Death-is-the-real-truth Commented Jul 8, 2017 at 5:37 -
2
you want
JSON.stringify()
capital letters – Wesley Smith Commented Jul 8, 2017 at 5:37 - I'm voting to close this question as off-topic because it looks like either a typographical error, or a rather localised question that may have been abandoned without an answer. – halfer Commented Oct 17, 2017 at 17:34
2 Answers
Reset to default 5It 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.