Results 1 to 4 of 4

Thread: Ajax and WebMethod Function are not in sync

  1. #1

    Thread Starter
    Lively Member
    Join Date
    Aug 2010
    Posts
    87

    Ajax and WebMethod Function are not in sync

    Ajax and WebMethod Function are not in sync.

    Ignoring the json input and testing if the string gets past gives this error:

    Error Code:[object Object] Status: 401 Ex: Unauthorized
    Here is my Webmethod and we are ignoring mo, dy and yr as a json input:
    Code:
    Imports System.IO
    Imports System.Web.Services
    
    Public Class _Default
        Inherits Page
    
        <WebMethod()>
        Public Shared Function GetDate(mo As Integer, dy As Integer, yr As Integer) As String
            Return "Test"
        End Function
    End Class
    This is my Ajax call. I have the exact same code in C# and it works as prescribed:

    Code:
        <script type="text/javascript" src="~/jquery=1.10.2.js"></script>
        <script type="text/javascript">
            $(document).ready(function () {
                $("#<%= Button1.ClientID %>").click(function () {
                    var mo1 = $("#<%= TextBox1.ClientID %>").val();
                    var dy1 = $("#<%= TextBox2.ClientID %>").val();
                    var yr1 = $("#<%= TextBox3.ClientID %>").val();
                    var data = { mo: mo1, dy: dy1, yr: yr1 };
                    var json1 = JSON.stringify(data); 
                    $.ajax
                    ({
                        type: "POST",
                        contentType: "application/json; charset=utf-8",
                        dataType: "json",
                        data: json1,
                        url: "Default.aspx/GetDate",
                        success: function (result) {
                            $("#<%= TextBox4.ClientID %>").val(result.d);
                        },
                            error: function (status, ex) {
                                alert("Error Code:" + status + "Status: " + status.status + " Ex: " + status.statusText);
                        }
                    });
                    return false;
                });
            });
        </script>
    Last edited by Philosophaie; Apr 28th, 2017 at 12:43 AM.

  2. #2
    Frenzied Member KGComputers's Avatar
    Join Date
    Dec 2005
    Location
    Cebu, PH
    Posts
    2,020

    Re: Ajax and WebMethod Function are not in sync

    Did you set the authorization tag in web.config to private? or just public?

    - kgc
    CodeBank: VB.NET & C#.NET | ASP.NET
    Programming: C# | VB.NET
    Blogs: Personal | Programming
    Projects: GitHub | jsFiddle
    ___________________________________________________________________________________

    Rating someone's post is a way of saying Thanks...

  3. #3

    Thread Starter
    Lively Member
    Join Date
    Aug 2010
    Posts
    87

    Re: Ajax and WebMethod Function are not in sync

    No authorization tag. Where is it located in Web.Config?

  4. #4
    Frenzied Member KGComputers's Avatar
    Join Date
    Dec 2005
    Location
    Cebu, PH
    Posts
    2,020

    Re: Ajax and WebMethod Function are not in sync

    something like this. Note that this will allow the resource to be public.

    Code:
    <system.web>
      <authorization>
        <allow users="*"/>
      </authorization>
    </system.web>
    CodeBank: VB.NET & C#.NET | ASP.NET
    Programming: C# | VB.NET
    Blogs: Personal | Programming
    Projects: GitHub | jsFiddle
    ___________________________________________________________________________________

    Rating someone's post is a way of saying Thanks...

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width