简体   繁体   中英

Cannot reach PageMethod with Ajax

I'm working on online quiz project. User will select number of questions,questions and selections will be drawn from db randomly in Page_Load method. User will chic mark. Then when user clicks submit button, user's choice will be sent to static pagemethod through AJAX. I am a beginner and couldn't managed to send datas with Ajax to PageMethod despite a day's work. I need your help. Here are my codes :

    <div id="selections">
                        <asp:ScriptManager ID="ScriptManager1" runat="server"></asp:ScriptManager>
                        <asp:UpdatePanel ID="UpdatePanel1" runat="server">
                            <ContentTemplate>
                                <input type="radio" name="secenekler" value="1" id="input1" />
                                <label class="row qaelement labelClick" for="input1">
                                    <asp:Label ID="TextSecenekA" CssClass="textsecenek" runat="server"></asp:Label>
                                </label>

                                <input type="radio" name="secenekler" value="2" id="input2" />
                                <label class="row qaelement labelClick" for="input2">
                                    <asp:Label ID="TextSecenekB" CssClass="textsecenek" runat="server"></asp:Label>
                                </label>

                                <input type="radio" name="secenekler" value="3" id="input3" />
                                <label class="row qaelement labelClick" for="input3">
                                    <asp:Label ID="TextSecenekC" CssClass="textsecenek" runat="server"></asp:Label>
                                </label>

                                <input type="radio" name="secenekler" value="4" id="input4" />
                                <label class="row qaelement labelClick" for="input4">
                                    <asp:Label ID="TextSecenekD" CssClass="textsecenek" runat="server"></asp:Label>
                                </label>

                                <div class="row">
                                    <asp:Button ID="SoruGonder" ClientIDMode="Static" class="btn btn-primary btn-lg offset-md-4 col-md-4" runat="server" Text="My Last Decision!" />
                                </div>
                            </ContentTemplate>
                        </asp:UpdatePanel>
</div>

JS:

<script type="text/javascript">
    function getRadioValue(theRadioGroup) {
        var elements = document.getElementsByName(theRadioGroup);
        for (var i = 0, l = elements.length; i < l; i++) {
            if (elements[i].checked) {
                return elements[i].value;
            }
        }
        return null;
    }

    $(document).ready(function () {

        $('#SoruGonder').click(function () {
            var ChoosenChicValue = getRadioValue('secenekler');
            if (ChoosenChicValue == null) {
                alert("Please Make A Choice");
            }
            else {
                $.ajax({
                    type: 'POST',
                    url: '/quiz.aspx/quizpagemethod',
                    data: ChoosenChicValue,
                    contentType: 'application/json; charset=utf-8',
                    datatype: 'json',
                    success: function (data) {
                        document.getElementById('<%= TextSoru.ClientID %>').value = data.question;
                        document.getElementById('<%= TextSecenekA.ClientID %>').value = data.selection1;
                        document.getElementById('<%= TextSecenekB.ClientID %>').value = data.selection2;
                        document.getElementById('<%= TextSecenekC.ClientID %>').value = data.selection3;
                        document.getElementById('<%= TextSecenekD.ClientID %>').value = data.selection4;
                    },
                    error: function () {
                        alert("Ajax Error");
                    }
                });
            }
        });
    });
</script>






  public class SoruCevapClass
    {
        public string question { get; set; }
        public string selection1 { get; set; }
        public string selection2 { get; set; }
        public string selection3 { get; set; }
        public string selection4 { get; set; }
    }

    [System.Web.Services.WebMethod(EnableSession = true)]
    protected static string GreetingBtn_Click(string PostedAnswer)
    {
        System.Diagnostics.Debug.WriteLine("POSTED ANSWER : "+PostedAnswer); //This is not printing on output.
//...
     }

在您的ScriptManager中添加以下代码:EnablePageMethods =“ true”

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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