2014-01-25 60 views
1

这是我的代码背后:无法访问背后asp.net用ajax后的代码

enter image description here

这是我的KO脚本:

enter image description here

这是结果当我点击我的提交按钮 enter image description here

我的代码有什么问题吗?

+0

这个错误对我来说似乎很清楚。你如何设置你的认证? – CodeCaster

+0

我试图把本 <位置路径= “SupplyPoints.aspx”> <授权> <允许用户= “*”/> <认证模式=”无“/> – comfreakph

+0

这是一个香草的web应用程序,还是通过某种类型的CMS运行? –

回答

1

我已经下载了您的解决方案,并得到了它的工作

App_Start\RouteConfig.cs你有下面这行需要被删除:

settings.AutoRedirectMode = RedirectMode.Permanent; 

而且您的Web方法必须static

+0

仍然有auth失败信息 – comfreakph

+0

我已更新我的回答 –

+0

谢谢。它现在正在工作 – comfreakph

1

App_Start\RouteConfig.cs 更改

settings.AutoRedirectMode = RedirectMode.Permanent; 

settings.AutoRedirectMode = RedirectMode.Off; 

如果Web方法不是在后面的代码静态的,它不会工作。
如果你真的想继续使用代码,你可以通过创建一个静态方法来实现。
实施例:

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

一种更容易的替代方法是创建一个使用实例方法web服务(.ASMX)。