I’m new for Jquery. I’m trying to call server method from my user control with Jquery ajax. It is wired for me that when the server page that the request is sent to is located in root path, ajax works. But when I moved the page into a subfolder and changed the url parameter of Jquery ajax, nothing happened…
Below is the web method in WebForm1.aspx.cs:
namespace WebApplication11
{
public partial class WebForm1 : System.Web.UI.Page
{
[System.Web.Services.WebMethod]
public static string test2()
{
return "TestString";
}
protected void Page_Load(object sender, EventArgs e)
{
}
}
}
When the page is located under root, it works. My ajax function in the user control is here:
<%@ Control Language="C#" AutoEventWireup="true"
CodeBehind="WebUserControl1.ascx.cs"
Inherits="WebApplication11.NewFolder1.WebUserControl1" %>
<script type ="text/javascript">
function ajaxTest()
{
$.ajax({
type: "post",
url: "WebForm1.aspx/test2",
data: "{}",
contentType: "Application/json; charset=utf-8",
dataType: "json",
success: function (r) {alert(r.d);}
})
}
</script>
<input type = "checkbox" id = "c1" onchange = "ajaxTest()" />
But after I move the WebForm1.aspx page from root to a subfolder “NewFolder2”, and changed the “url” in ajax method from “WebForm1.aspx/test2” to “/NewFolder2/WebForm1.aspx/test2” or
“NewFolder2/WebForm1.aspx/test2”, nothing happened.
I am frustrated now. Can anybody give me a hint to figure it out? Thank you very much!
Are you putting the path from the controls folder to the ajax method or from the page the control is put on? When a control is placed on a page, its ajax method calls must follow the path as if it was on the page itself because that is how it is rendered.