简体   繁体   English

如何在AJAX成功连接中调用函数背后的代码

[英]How to call code behind function in success junction of AJAX

I'm consuming a web service using ajax in web form 3.5, then save it to database. 我正在使用Web表单3.5中的Ajax使用Web服务,然后将其保存到数据库中。 I'm getting a success response from the web service. 我从Web服务获得成功响应。 However, how will I call my code behind function to save the data after success in ajax? 但是,在ajax成功后,我将如何调用函数背后的代码来保存数据? I'm new to 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>

you just should to call another ajax again in success section. 您只应该在成功部分再次调用另一个ajax。 like this : 像这样 :

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) {

                        }
                    });  
    }

I hope it has been helpful . 希望对您有所帮助。

Ajax code: 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); } }); }); 

For these type of problems. 对于这些类型的问题。 It's good to create a separate Js file as it keeps the code clean and understandable. 最好创建一个单独的Js文件,因为它可以使代码清晰易懂。 And please be more precise with the questions. 并且请更精确地回答问题。

From my understanding => if this ie " https://webservice.com/register " one sends a success response, you need to save it to say " https://localhost/register " in your own system. 以我的理解=>如果此“ https://webservice.com/register ”发送成功响应,则需要将其保存为在您自己的系统中说“ https:// localhost / register ”。 create a varible url in the global scope of your field and set that url to your ajax req. 在您的字段的全局范围内创建一个可变网址,并将该网址设置为您的ajax请求。

 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 

You can directly use shor-hand method of ajax for post request. 您可以直接使用ajax的shor-hand方法进行发布请求。 $(selector).post(URL,data,function(data,status,xhr),dataType); $(selector).post(URL,data,function(data,status,xhr),dataType); For Example in your case: 例如您的情况:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM