文件名称:webservice-ajax
文件大小:39KB
文件格式:ZIP
更新时间:2010-10-29 16:15:03
ajax webservice
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.
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
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
【文件预览】:
WebServiceCallApp
----WebServiceCallApp()
--------WebServices()
--------Default.aspx.designer.cs(1KB)
--------WebServiceCallApp.csproj(3KB)
--------App_Code()
--------Properties()
--------obj()
--------Default.aspx.cs(591B)
--------WebServiceCallApp.csproj.user(1KB)
--------Default.aspx(1KB)
--------bin()
--------Web.config(6KB)
----WebServiceCallApp.sln(1KB)
----WebServiceCallApp.suo(17KB)
webservice-ajax.doc