与使用jQuery POST表单数据到ASP.NET AJAX ASMX Web服务错误 [英] Error with Using jQuery to POST Form Data to an ASP.NET ASMX AJAX Web Service

查看:245
本文介绍了与使用jQuery POST表单数据到ASP.NET AJAX ASMX Web服务错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想实现那个帖子一个整体的形式jQuery的AJAX方法。现在,我得到了jQuery成功执行了服务器端code,但在服务器端变量NameValue [] formvars是一个空的!我想不通为什么。谁能帮助这个问题。谢谢

I am trying to implement a jquery ajax method that post a whole form. Now I got the jquery successfully execute the server side code, but the server side variable NameValue[] formVars is empty !! I can't figure out why. Could anyone help with this issue. thanks

但我的jQuery ajax调用总是返回空白错误。

But my jquery ajax call always return blank error.

下面是我的javascript code

Here is my javascript code

 <script type="text/javascript">

    $(document).ready(function () {


        var MaxInputs = 8; //maximum input boxes allowed
        var InputsWrapper = $("#InputsWrapper"); //Input boxes wrapper ID
        var AddButton = $("#AddMoreFileBox"); //Add button ID

        var x = InputsWrapper.length; //initlal text box count
        var FieldCount = 1; //to keep track of text box added

        $(AddButton).click(function (e)  //on add input button click
        {
            if (x <= MaxInputs) //max input box allowed
            {
                FieldCount++; //text box added increment
                //add input box
                $(InputsWrapper).append('<div><input type="text" name="mytext[]" id="field_' + FieldCount + '" value="Text ' + FieldCount + '"/><a href="#" class="removeclass">&times;</a></div>');
                x++; //text box increment
            }
            return false;
        });

        $("#main_container").on("click", ".removeclass", function (e) { //user click on remove text
            //alert("clicked");
            if (x > 1) {
                $(this).parent('div').remove(); //remove text box
                x--; //decrement textbox
            }
            return false;
        })


        // Add the page method call as an onclick handler for the div.
        $("#Result").click(function () {
            $.ajax({
                type: "POST",
                url: "testingJqueryAjax.aspx/GetDate",
                data: "{'msg':'hello'}",
                contentType: "application/json; charset=utf-8",
                dataType: "json",
                success: function (msg) {
                    // Replace the div's content with the page method's return.
                    $("#Result").text(msg.d);
                }
            });
        });
        function sendRegistration() {
            var arForm = $("#form1").serializeArray();

            $.ajax({
                type: "POST",
                url: "testingJqueryAjax.aspx/ExecuteRegistration",
                contentType: "application/json",
                data: JSON.stringify({ formVars: arForm }),
                dataType: "json",
                success: function (result) {
                     var jEl = $("#msgdiv");
                    jEl.html(result.d).fadeIn(1000);
                    setTimeout(function () { jEl.fadeOut(1000) }, 5000);
                   // $("#msgdiv").text(result.d);
                },
                error: function (ts) {
                    alert("An error occurred: " + ts.responseText);
                }
            });
        }


        $("#btnSend").click(function () {
            alert("btnSend clicked");
            sendRegistration();
        });
    });
</script>

下面是我的服务器端code

Here is my server side code

 [WebMethod]
    public static string ExecuteRegistration(NameValue[] formVars)
    {
        StringBuilder sb = new StringBuilder();

        sb.AppendFormat("Thank you {0}, <br/><br/>",
                        HttpUtility.HtmlEncode(formVars.Form("txtName")));

        sb.AppendLine("You've entered the following: <hr/>");

        foreach (NameValue nv in formVars)
        {
            // strip out ASP.NET form vars like _ViewState/_EventValidation
            if (!nv.name.StartsWith("__"))
            {
                if (nv.name.StartsWith("txt") || nv.name.StartsWith("lst") || nv.name.StartsWith("chk"))
                    sb.Append(nv.name.Substring(3));
                else
                    sb.Append(nv.name);
                sb.AppendLine(": " + HttpUtility.HtmlEncode(nv.value) + "<br/>");
            }
        }
        sb.AppendLine("<hr/>");

        string[] needs = formVars.FormMultiple("lstSpecialNeeds");
        if (needs == null)
            sb.AppendLine("No Special Needs");
        else
        {
            sb.AppendLine("Special Needs: <br/>");
            foreach (string need in needs)
            {
                sb.AppendLine("&nbsp;&nbsp;" + need + "<br/>");
            }
        }

        return sb.ToString();
    }

下面是HTML

<hr />
 <div id="msgdiv">
    divmsg : 
</div>
<div id="div1" class="errordisplay" style="display: none">
</div>

<div>
    <div class="label">Name:</div>
    <div>
        <asp:TextBox runat="server" ID="txtName" />
    </div>
</div>
<div>
    <div class="label">Company:</div>
    <div>
        <asp:TextBox runat="server" ID="txtCompany" />
    </div>
</div>
<div>
    <div class="label"></div>
    <div>
        <asp:DropDownList runat="server" ID="lstAttending">
            <asp:ListItem Text="Attending" Value="Attending" />
            <asp:ListItem Text="Not Attending" Value="NotAttending" />
            <asp:ListItem Text="Maybe Attending" Value="MaybeAttending" />
            <asp:ListItem Text="Not Sure Yet" Value="NotSureYet" />
        </asp:DropDownList>
    </div>
</div>
<div>
    <div class="label">
        Special Needs:<br />
        <small>(check all that apply)</small>
    </div>
    <div>
        <asp:ListBox runat="server" ID="lstSpecialNeeds" SelectionMode="Multiple">
            <asp:ListItem Text="Vegitarian" Value="Vegitarian" />
            <asp:ListItem Text="Vegan" Value="Vegan" />
            <asp:ListItem Text="Kosher" Value="Kosher" />
            <asp:ListItem Text="Special Access" Value="SpecialAccess" />
            <asp:ListItem Text="No Binder" Value="NoBinder" />
        </asp:ListBox>
    </div>
</div>
<div>
    <div class="label"></div>
    <div>
        <asp:CheckBox ID="chkAdditionalGuests" Text="Additional Guests" runat="server" />
    </div>
</div>

<hr />

<input type="button" id="btnSend" value="Send Registration" />

我按照<一href="http://weblog.west-wind.com/posts/2010/Sep/07/Using-jQuery-to-POST-Form-Data-to-an-ASPNET-ASMX-AJAX-Web-Service"相对的,而不是使用ASMX =nofollow的>教程以下链接的,我用的aspx静态方法。现在运行code,我可以成功执行服务器端code,但它不会从网络表单拿起任何数据。

I follow the tutorialon the following link, instead of using asmx, I used aspx static method. now running the code I can successfully execute the server side code, but it does not pick up any data from the webform.

在NameValue [] formVars变量没有条目。为什么?

The NameValue[] formVars variable has no entries. WHY??

在这方面的更新,我已经把code空白aspx页面上,那么程序的工作原理。本来我把它建成一个母版页上一个aspx页面上。因此,它似乎有事情做与母版页。

An update on this, I have put the code on a blank aspx page, then the program works. Originally I put it on an aspx page built on a master page. So it appears to have something to do with the master page.

推荐答案

我已成功地解决了这个问题,这个问题是由我的测试页从继承母版页上的Java脚本引用造成的。

I have managed to solve this problem, the issue was caused by java script references on the master page that my testing page is inherited from.

这篇关于与使用jQuery POST表单数据到ASP.NET AJAX ASMX Web服务错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆