locked
Problem to send data to server using ajax Request and webMethod RRS feed

  • Question

  • User-79977429 posted

    Hi

    in mu asp.net wobform project, i want to send some data to server via ajax request and webMethod, here is my simple code :

    function PostData(userName) {
                    $.ajax({
                        type: 'POST',
                        url: 'WebForm1.aspx/IsValidUser',
                        contentType: 'application/json; charset=utf-8',
                        dataType: 'json',
                        data: { strUserName: userName },
                        sucess: function (response) {
                            alert('success!');
                        },
                        failure: function (response) {
                            alert('failed!');
                        },
                        error: function (xhr, textStatus, errorThrown) {
                            alert('error!');
                        }
                    });
                }

    Here is my codeBehind :

    [WebMethod]
            public static bool IsValidUser(string strUserName)
            {
                bool bResult = false;
    
                if (strUserName == "ali")
                    bResult = true;
    
                return bResult;
            }

    But in runTime, always error function is occures! Does something wrong?

    thanks in advance

    Saturday, December 23, 2017 4:47 PM

Answers

  • User475983607 posted

    Working example

    <%@ Page Language="C#" AutoEventWireup="true" CodeBehind="AjaxDemo.aspx.cs" Inherits="WebFormsDemo.AjaxDemo" %>
    
    <!DOCTYPE html>
    
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head runat="server">
        <title></title>
        <script src="https://code.jquery.com/jquery-3.2.1.js"
            integrity="sha256-DZAnKJ/6XZ9si04Hgrsxu/8s717jcIzLy3oi35EouyE="
            crossorigin="anonymous">
        </script>
        <script>
    
            $(function () {
    
                $('#<%=Button1.ClientID%>').click(function (e) {
    
                    e.preventDefault();
    
                    var data = {};
                    data.strUserName = $('#<%=TextBox1.ClientID%>').val();
    
                    $.ajax({
                        url: 'AjaxDemo.aspx/IsValidUser',
                        type: 'POST',
                        data: JSON.stringify(data),
                        contentType: 'application/json;utf-8',
                        datatype: 'json'
                    }).done(function (data) {
                        console.log(data);
                    }).fail(function (data) {
                        console.log("Error: " + data);
                    });
                });
    
            });
        </script>
    
    </head>
    <body>
        <form id="form1" runat="server">
            <asp:TextBox ID="TextBox1" runat="server">ali</asp:TextBox>
            <asp:Button ID="Button1" runat="server" Text="Button" />
        </form>
    </body>
    </html>
    
            [WebMethod]
            public static bool IsValidUser(string strUserName)
            {
                return "ali" == strUserName;
            }

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Saturday, December 23, 2017 7:45 PM