Jquery 调用asp.net ajax (web service/static page method)的示例(一)---简单参数
示例一(无参数的情况) :
$.ajax({ type: "POST", contentType: "application/json; charset=utf-8", url: "WebService.asmx/WebMethodName", data: "{}", //当被调用的page method 或则 web service 的方法是无参情况时候,需要传入空的json 对象 dataType: "json"});
示例二(简单参数的情况) :
$.ajax({ type: "POST", url: "../Services/UCRender.asmx/GetControl", data: "{'path':'~/UserControls/RSSReaderControl.ascx'}", contentType: "application/json; charset=utf-8", dataType: "json", success: function (msg) { $('#RSSContent').removeClass('loading'); $('#RSSContent').html(msg.d); } }); }
对应的service 方法如下:
/// <summary>
/// Summary description for UCRender
/// </summary>
[WebService(Namespace = "http://tempuri.org/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
// To allow this Web Service to be called from script, using ASP.NET AJAX, uncomment the following line.
[System.Web.Script.Services.ScriptService]
public class UCRender : System.Web.Services.WebService
{
[WebMethod]
public string GetControl(string path)
{
Page page = new Page();
UserControl ctl = (UserControl)page.LoadControl(path);
page.Controls.Add(ctl);
StringWriter writer = new StringWriter();
HttpContext.Current.Server.Execute(page, writer, false);
return writer.ToString();
}
}