javascript - ReferenceError: json is not defined when AJAX call the server method - Stack Overflow

admin2025-04-08  0

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

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 3
  • json.stringify need to be JSON.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
Add a ment  | 

2 Answers 2

Reset to default 5

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.

转载请注明原文地址:http://conceptsofalgorithm.com/Algorithm/1744124174a232385.html

最新回复(0)