开发工具:
文件大小: 39kb
下载次数: 0
上传时间: 2007-10-09
详细说明:
In order to AJAX enable it we will have to add the following attribute to the Web Service declaration part:
[System.Web.Script.Services.ScriptService()]
When this is done our Web Service is ready to respond to client-side JavaScript calls.< br>One more thing has to be done here: we need the Web Method that we will call from client-side JavaScript. Let us define it like this:
[WebMethod]
public string GetServerResponse(string callerName)
{
if(callerName== string.Empty)
throw new Exception("Web Service Exception: invalid argument");
return string.Format("Service responded to {0} at {1}", callerName, DateTime.Now.ToString());
}
Configuring ASP.Net Application
ASP.Net applications web.config file also has to be modified in order to enable Web Service calls from client-side JavaScript code.
This modification is though made for you by Microsoft Visual Studio 2005 if you are using ASP.Net AJAX template. Here is the example of what can be inserted into httpHandles section of your web.config file:
...
...
...
...
...
Making client-side JavaScript code
Let us take a look at the default.aspx file that was automatically created in our project (if it was not - then you will have to add one manually). First we have to make sure that we have one and only one instance of Script Manager object on your page:
Then we have to add Services collection to our ScriptManager object, add ServiceReference to the Services collection and specify Path to the desired service.
The result might look like this:
or like this if you prefer to do it directly in your code-behind file :
ScriptManager1.Services.Add(new ServiceReference("~/WebServices/MyWebService.asmx"));
Now we need some client-side functions, button to trigger Web Service request and a text box to provide the input for the Web Service:
• SendRequest - this function will send asyncroneus request to the Web Service
• OnComplete - this function will receive result from the Web Service
• OnError - this function will be triggered if an error occures while executing Web Service
• OnTimeOut - this function will be triggered if Web Service will not respond
• MyTextBox- text box with the input for the Web Service
• RequestButton - the button that triggers SendRequest function
The result might look like this:
Collapse
Web Service call from client-side JavaScript
This is basically it. You have a functioning client-side JavaScript code that calls server-side Web Service and treats returned response.
If we supply empty input value to the GetServerResponse method of MySampleService then as expected it will throw an exception. This exception will be caught by the OnError function in the client-side JavaScript:
Conclusion
The described client-to-server communication model where client-side JavaScript code invokes Web Service methods provides us with a lot of flexibility to extend the web site functionality. At the same time the traffic is minimal: we send only the input data required by the Web Method and we receive only the return value from the method being invoked.
History
You can always find the most up-to-date version of this article in the AJAX section of my web site.
About semenoff
...展开收缩
(系统自动生成,下载前可以参看下载内容)
下载文件列表
相关说明
- 本站资源为会员上传分享交流与学习,如有侵犯您的权益,请联系我们删除.
- 本站是交换下载平台,提供交流渠道,下载内容来自于网络,除下载问题外,其它问题请自行百度。
- 本站已设置防盗链,请勿用迅雷、QQ旋风等多线程下载软件下载资源,下载后用WinRAR最新版进行解压.
- 如果您发现内容无法下载,请稍后再次尝试;或者到消费记录里找到下载记录反馈给我们.
- 下载后发现下载的内容跟说明不相乎,请到消费记录里找到下载记录反馈给我们,经确认后退回积分.
- 如下载前有疑问,可以通过点击"提供者"的名字,查看对方的联系方式,联系对方咨询.