1

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

3
  • json.stringify need to be JSON.stringify Commented Jul 8, 2017 at 5:37
  • 2
    you want JSON.stringify() capital letters 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. Commented Oct 17, 2017 at 17:34

2 Answers 2

5

It should be JSON.stringify, not json.stringify

Sign up to request clarification or add additional context in comments.

9 Comments

probly best as a comment
@DelightedD0D But what if that's an answer?
@bugwheels94 it is the "answer" but this is is simple typographical error. Generally, we'd just comment the answer and close the question as it has no future value
@DelightedD0D i agreed with you.But every-one doing this (answer any-how). Only some people follow what you said.So leave it.
@michelle if I am doing the JSON.stringify then it coming other error i.e. "TypeError: c.get_textboxcontrol is not a function". Now whats going wrong???
|
0
 <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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.