【问题标题】:How to call code behind function in success junction of AJAX如何在 AJAX 成功结点中调用函数后面的代码
【发布时间】:2019-09-11 01:43:35
【问题描述】:

我正在使用 Web 表单 3.5 中的 ajax 使用 Web 服务,然后将其保存到数据库中。我从 Web 服务获得了成功响应。但是,在ajax成功后,我将如何调用我的代码隐藏函数来保存数据?我是 ajax 新手。

<body>
    <form id="form1" runat="server">
    <div id ="divName" runat="server" class="form-group">
                <label class="control-label">Name</label>
                <asp:TextBox ID="txtname" CssClass="form-control" runat="server" ></asp:TextBox>
            </div>
    <input id="btnRegister" type="button" value="Register" /><br />
    </form>
    <script type ="text/javascript">
        $(document).ready(function () {
            $("#btnRegister").click(function () {
                debugger;
                var namee = document.getElementById("<%=txtname.ClientID %>").value;
                Register(namee);
            });

            function Register(name) {
                debugger;
                var id = 
                $.ajax({
                    type: "POST",
                    url: "https://webservice.com/register",
                    data: JSON.stringify({
                        "name": name
                    }),
                    contentType: "application/json",
                    datatype: "json",
                    success: function (response) {
                        //call code behind function in saving data.
                    },
                    failure: function (response) {
                        alert("ERROR");
                    }
                });
            }
        });
    </script>
</body>

【问题讨论】:

  • 您知道您可以使用传统帖子在您的代码中调用 Web 服务吗?如果您想调用您的代码隐藏客户端,您需要将其设置为 WebMethod,然后也使用 AJAX 调用它。
  • 还要注意在 ASP.net 中这些被称为 PageMethods,更多信息在这里:dotnetcurry.com/ShowArticle.aspx?ID=109
  • @JonP,是的,我知道这一点,但是我的代码中有很多验证,所以我的方法是将 Web 服务的消费和另一个将数据保存到我的数据库的功能分开。
  • 验证是好的,但是为什么在验证失败的情况下调用webservice呢?
  • @JonP,但这是程序的一部分。

标签: c# jquery asp.net ajax


【解决方案1】:

您应该在成功部分再次调用另一个 ajax。 像这样:

function Register(name) {
                debugger;
                var id = 
                $.ajax({
                    type: "POST",
                    url: "https://webservice.com/register",
                    data: JSON.stringify({
                        "name": name
                    }),
                    contentType: "application/json",
                    datatype: "json",
                   success: function (response) {
                         MySaveDataFunc(response)
                    },
                    failure: function (response) {
                        alert("ERROR");
                    }
                });
            }



 var MySaveDataFunc=function(mydata)
    {
                    $.ajax({
                        type: "POST",
                        url: "Your SaveData Url",
                        data: mydata
                        contentType: "application/json",
                        datatype: "json",
                        success: function (response) {                         
                        },
                        failure: function (response) {

                        }
                    });  
    }

希望对你有所帮助。

【讨论】:

    【解决方案2】:

    Ajax 代码:

    $(document).ready(function () {
        $.ajax({
            url: "Registration.aspx/GetData",
            type:"POST",
            contentType: "application/json; charset=utf-8",
            dataType: "json",
            success: function (data) {
                $('#responseContent').text(data.d);
            }
    
        });
    });

    【讨论】:

      【解决方案3】:

      针对这类问题。最好创建一个单独的 Js 文件,因为它可以使代码保持干净和易于理解。 请更准确地回答问题。

      根据我的理解 => 如果这即“https://webservice.com/register”发送成功响应,您需要将其保存为您自己的系统中的“https://localhost/register”。 在您的字段的全局范围内创建一个可变 url 并将该 url 设置为您的 ajax req。

      var yourUrl = "something";
      
      //on ajax send method
      url: yourUrl
      
      //on success method, call the same method but change the url to your new url, this way you save both the time and redundant code
         Register(name,yourUrl); //add a second parameter to take url in your register function

      【讨论】:

        【解决方案4】:

        您可以直接使用ajax 的简写方法进行post 请求。 $(selector).post(URL,data,function(data,status,xhr),dataType); 例如你的情况:

        $.post(url:"https://webservice.com/register"
              ,data:JSON.stringify({"name": name})
              ,function(data,status,xhr){
              //Your callback function
              //Example: alert(data.Message);
              });
        

        【讨论】:

          猜你喜欢
          • 2016-05-14
          • 1970-01-01
          • 1970-01-01
          • 2017-08-25
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2012-12-21
          • 1970-01-01
          相关资源
          最近更新 更多