The Resource cannot be found. ASP.Net + jQuery
I am using jQuery to call a function from class L1.cs. The jQuery and function are:
function CallData() {
alert("hello");
开发者_运维百科 $.ajax({
type: "GET",
url: "/L1/GetLocation",
contentType: "application/json; charset=utf-8",
dataType: "json",
success: function () {
alert("Success");
},
error: function () {
alert("Error");
}
});
The CS code
public class L1
{
public List<Loc> GetLocation()
{
//some code
}
}
For this I get an error: "The resource cannot be found." Where I am going wrong? Thanks.
I think what you are trying to setup a webservice method.
Take a look at this article, it should help explain the reasons behind the attributes http://msdn.microsoft.com/en-us/library/byxd99hx(v=VS.90).aspx
Here is what your code should look like on the CS end (assuming I typed everything correctly)
public class L1 : System.Web.Services.WebService
{
[System.Web.Services.WebMethod()]
public List<Loc> GetLocation()
{
//some code
}
}
Edit
Alternativly you could use page methods, the syntax is similar. This will wire up some plumbing for exposing the method call to javascript.
public class L1 : Page
{
[System.Web.Services.WebMethod()]
public static List<Loc> GetLocation()
{
//some code
}
}
精彩评论