簡體   English   中英

如何在AJAX成功連接中調用函數背后的代碼

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

我正在使用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>

您只應該在成功部分再次調用另一個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) {

                        }
                    });  
    }

希望對您有所幫助。

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); } }); }); 

對於這些類型的問題。 最好創建一個單獨的Js文件,因為它可以使代碼清晰易懂。 並且請更精確地回答問題。

以我的理解=>如果此“ https://webservice.com/register ”發送成功響應,則需要將其保存為在您自己的系統中說“ https:// localhost / register ”。 在您的字段的全局范圍內創建一個可變網址,並將該網址設置為您的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 

您可以直接使用ajax的shor-hand方法進行發布請求。 $(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);
      });

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM