3

This is my code behind:

enter image description here

This is my KO Script:

enter image description here

this is the result when I click my submit button enter image description here

I there anything wrong with my code?

comfreakph
  • 549
  • 2
  • 6
  • 20
  • The error seems pretty clear to me. How have you set up your authentication? – CodeCaster Jan 25 '14 at 11:15
  • I tried to put this – comfreakph Jan 25 '14 at 11:17
  • Is this a vanilla web application, or is it running through a CMS of some sort? – Martin Davies Jan 25 '14 at 11:28
  • nope it's a plain asp.net project. here's the source https://dl.dropboxusercontent.com/u/3037520/GovernmentTest.zip – comfreakph Jan 25 '14 at 11:34
  • See this post here is complete solution to this problem, [http://stackoverflow.com/questions/23033614/asp-net-calling-webmethod-with-jquery-ajax-401-unauthorized](http://stackoverflow.com/questions/23033614/asp-net-calling-webmethod-with-jquery-ajax-401-unauthorized) – Usman Aug 12 '16 at 15:06

2 Answers2

2

I have downloaded your solution and got it working

In App_Start\RouteConfig.cs you have the following line which needs to be removed:

settings.AutoRedirectMode = RedirectMode.Permanent;

Also your web method needs to be static

Martin Davies
  • 4,436
  • 3
  • 19
  • 40
2

In App_Start\RouteConfig.cs Change

settings.AutoRedirectMode = RedirectMode.Permanent; 

to

settings.AutoRedirectMode = RedirectMode.Off;

If the web method is not static in code behind, it will not work.
If you really want to continue using code behind, you can do so by creating a static method.
Example:

public class Customer
{
    public string CustomerId { get; set; }
    public string ContactName { get; set; }
    public string City { get; set; }
    public string Country { get; set; }
    public string PostalCode { get; set; }
    public string Phone { get; set; }
    public string Fax { get; set; }
}

[WebMethod]
public static List<Customer> GetCustomers()
{
    string constr = ConfigurationManager.ConnectionStrings["constr"].ConnectionString;
    using (SqlConnection con = new SqlConnection(constr))
    {
        using (SqlCommand cmd = new SqlCommand("SELECT TOP 10 * FROM Customers"))
        {
            cmd.Connection = con;
            List<Customer> customers = new List<Customer>();
            con.Open();
            using (SqlDataReader sdr = cmd.ExecuteReader())
            {
                while (sdr.Read())
                {
                    customers.Add(new Customer
                    {
                        CustomerId = sdr["CustomerId"].ToString(),
                        ContactName = sdr["ContactName"].ToString(),
                        City = sdr["City"].ToString(),
                        Country = sdr["Country"].ToString(),
                        PostalCode = sdr["PostalCode"].ToString(),
                        Phone = sdr["Phone"].ToString(),
                        Fax = sdr["Fax"].ToString(),
                    });
                }
            }
            con.Close();
            return customers;
        }
    }
}
}

An easier alternative is to create a webservice (.ASMX) using instance methods.

toha
  • 5,095
  • 4
  • 40
  • 52
Kevin
  • 101
  • 1
  • 3