Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>The first thing that you should consider is the <a href="http://en.wikipedia.org/wiki/Same_origin_policy" rel="noreferrer">same origin policy</a> restriction. If you are not able to comply with it and your web service is not hosted on the same domain as the consuming AJAX script you may stop reading my answer here and rethink your architecture.</p> <p>If you are still reading you could start by defining the service contract and implementation as usual:</p> <pre><code>[ServiceContract] public interface IFoo { [OperationContract] string GetData(int value); } public class FooService : IFoo { public string GetData(int value) { return string.Format("You entered: {0}", value); } } </code></pre> <p>Then you add a <code>fooservice.svc</code> file which will expose the service in IIS:</p> <pre><code>&lt;%@ ServiceHost Language="C#" Debug="true" Service="SomeNs.FooService" CodeBehind="FooService.svc.cs" Factory="System.ServiceModel.Activation.WebScriptServiceHostFactory" %&gt; </code></pre> <p>The last line <code>Factory="System.ServiceModel.Activation.WebScriptServiceHostFactory"</code> is extremely important as this is what will allow you to use JSON.</p> <p>The last part is web.config:</p> <pre><code>&lt;system.serviceModel&gt; &lt;behaviors&gt; &lt;serviceBehaviors&gt; &lt;behavior&gt; &lt;serviceMetadata httpGetEnabled="true"/&gt; &lt;serviceDebug includeExceptionDetailInFaults="false"/&gt; &lt;/behavior&gt; &lt;/serviceBehaviors&gt; &lt;/behaviors&gt; &lt;serviceHostingEnvironment multipleSiteBindingsEnabled="true" /&gt; &lt;/system.serviceModel&gt; </code></pre> <p>And finally an HTML page sending AJAX request to consume the service:</p> <pre><code>&lt;!DOCTYPE html&gt; &lt;html&gt; &lt;head&gt; &lt;title&gt;WCF Test&lt;/title&gt; &lt;meta http-equiv="Content-Type" content="text/html; charset=utf-8" /&gt; &lt;script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js"&gt;&lt;/script&gt; &lt;script type="text/javascript" src="http://www.json.org/json2.js"&gt;&lt;/script&gt; &lt;script type="text/javascript"&gt; $(function () { $.ajax({ // Notice the URL here: Need to be hosted on the same domain url: '/fooservice.svc/getdata', type: 'post', contentType: 'application/json; charset=utf-8', data: JSON.stringify({ value: 7 }), success: function (result) { alert(result.d); } }); }); &lt;/script&gt; &lt;/head&gt; &lt;body&gt; &lt;/body&gt; &lt;/html&gt; </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

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