Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>Place the <a href="http://msdn.microsoft.com/en-us/library/system.web.ui.scriptmanager.aspx" rel="nofollow">ScriptManager</a> control on your page and add a reference to your <code>.asmx</code> service:</p> <pre><code>&lt;asp:ScriptManager ID="myManager" runat="server"&gt; &lt;Services&gt; &lt;asp:ServiceReference Path="~/MyService.asmx" /&gt; &lt;/Services&gt; &lt;/asp:ScriptManager&gt; </code></pre> <p>In the code-behind of your web-service declare you web method (notice the <a href="http://msdn.microsoft.com/en-us/library/system.web.script.services.scriptserviceattribute.aspx" rel="nofollow">ScriptService</a> attribute):</p> <pre><code>namespace MyServices { [WebService(Namespace = "http://tempuri.org/")] [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)] [System.ComponentModel.ToolboxItem(false)] [ScriptService] public class MyService : System.Web.Services.WebService { [WebMethod] public string SayHello(name) { return "Hello, " + name; } } } </code></pre> <p>Now you can consume the web-service from the Javascript like the following:</p> <pre><code>function queryWebService() { MyServices.MyService.SayHello('Bobby', function(result, userContext) { alert('Web-service response: ' + result); }, function(result) { alert('Error!'); }); } </code></pre> <p><strong>UPDATE</strong></p> <p>If you want to consume the web-service by simply sending an HTTP GET requests then you can do the following:</p> <p>Decorate your web-method with a <a href="http://msdn.microsoft.com/en-us/library/bb298830.aspx" rel="nofollow">ScriptMethod</a> attribute:</p> <pre><code>[WebMethod] [ScriptMethod(UseHttpGet = true)] public string SayHello(name) { return "Hello, " + name; } </code></pre> <p>Notice the <a href="http://msdn.microsoft.com/en-us/library/system.web.script.services.scriptmethodattribute.usehttpget.aspx" rel="nofollow">UseHttpGet</a> property which is set to <code>True</code>. You probably also need to modify the <code>web.config</code> file to allow this kind of interaction:</p> <pre><code>&lt;webServices&gt; &lt;protocols&gt; &lt;add name="HttpGet"/&gt; &lt;/protocols&gt; &lt;/webServices&gt; </code></pre> <p>Now you can make a simple HTTP GET request to your web-service like shown below (the example uses <a href="http://api.jquery.com/jQuery.ajax/" rel="nofollow">jQuery.ajax</a>):</p> <pre><code>$.ajax({ url: "/MyService.asmx/SayHello?name=Bobby", success: function(transport) { alert('Web-service response: ' + transport.responseText); } }); </code></pre> <p>Hope this will help you.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload