什么是从JavaScript调用一些业务逻辑上使用AJAX客户端的最有效的方法是什么?它看起来像你可以从JavaScript(在我的情况我使用jQuery来助阵)调用[WebMethod的]在一个aspx直接或者你可以直接调用的.asmx。其中要求会带来较少的开销?什么是最好的做法是什么?
What is the most efficient way of calling some business logic from javascript on the client side using AJAX? It looks like you can call a [WebMethod] on an aspx directly from javascript (in my case I'm using JQuery to help out) OR you can call a .asmx directly. Which call incurs less overhead? What is the best practice?
此外,什么是[ScriptService]属性做一个类?我从来没有在我的.aspx用这个[WebMethod的方法和一切似乎是工作的罚款。
Also, what does the [ScriptService] attribute do on a class? I have never used this before on my .aspx [WebMethod] methods and everything seems to be working fine.
我希望这是一个纯粹的客观问题。在此先感谢!
I'm hoping this is a purely objective question. Thanks in advance!
在ScriptService的东西,在我看来是一个隐藏的宝石在asp.net中。调用脚本进行维修,不要回传表单数据+视图状态,他们是瘦,快速JSON的有效载荷。
The ScriptService stuff in my opinion is a hidden gem in asp.net. Calls to the script service do not passback form data + viewstate, they are lean, fast JSON payloads.
继承人的最好的部分,ASP.NET3.5的ScriptManager的你能就产生一个JS适合你的方法调用,并建立所需要的任何JS类做的大部分工作。
Heres the best part, ASP.NET3.5's scriptmanager can do most of the work for you regarding generating a JS method for you to call and also setting up any JS classes needed.
一个简单的例子,包括获取信息的一个人,假设人是一个C#类。
A simple example for fetching details for a "Person", assuming Person is a C# class.
在PersonService.asmx:
namespace MyProj.Services {
[System.Web.Script.Services.ScriptService]
[System.Web.Script.Services.GenerateScriptType(typeof(Person))]
public class PersonService : System.Web.Services.WebService
{
[WebMethod, ScriptMethod(UseHttpGet = true, ResponseFormat = ResponseFormat.Json)]
public Person GetPersonDetails(int id)
{
/* return Logic here */
}
}
}
在DetailsPage.aspx
<asp:ScriptManager ID="ScriptManager1" runat="server">
<Services>
<asp:ServiceReference Path="~/Services/PersonService.asmx" />
</Services>
</asp:ScriptManager>
通过使用这样的设置,你甚至不会需要的JQuery的帮助来调用服务,并取回一个JS版本的C#Person类的功能,.NET也为你所有。 使用从JS这种服务的一个例子是:
By using a setup like this, you won't even need the help of JQuery to call the service and get back a JS version of your C# Person class, .net does that all for you. An example of using this service from JS would be:
MyProj.Services.PersonService.GetPersonDetails(id, _onDetailsCallbackSuccess, _requestFailed, null);
_onDetailsCallbackSuccess: function(result, userContext, methodName) {
//all persons properties are now intact and available
document.getElementById('txtFirstname').value = result.Firtname;
}
无论如何,这将是更多的则值得探讨的ASP.NET AJAX ScriptService的东西。即使你不打算使用它这一次它是一个pretty的邪恶的功能。
Anyway, it would be more then worth looking into the ASP.NET Ajax ScriptService stuff. Even if you decide not to use it this time it's a pretty wicked feature.
链接
看起来像使用scriptservice的一个很好的基础的例子:http://www.jankoatwarpspeed.com/post/2008/05/14/asp-net-ajax-basics-calling-scriptservices-using-javascript.aspx 在ASP.NET AJAX扩展器,以整合阿贾克斯到您的控制下一个合乎逻辑的步骤。 http://weblogs.asp.net/scottgu/archive/2007/08/19/using-asp-net-ajax-control-extenders-in-vs-2008.aspx Looks like a nice basic example of using a scriptservice: http://www.jankoatwarpspeed.com/post/2008/05/14/asp-net-ajax-basics-calling-scriptservices-using-javascript.aspx ASP.NET Ajax Extenders, the next logical steps to integrating Ajax into your controls. http://weblogs.asp.net/scottgu/archive/2007/08/19/using-asp-net-ajax-control-extenders-in-vs-2008.aspx