I have a very simple WCF Service called pilltrkr.svc. I\'m trying to call this service from jQuery via the following code:
var jsondata = JSON.stringif
Probably a bit late, but I wrote a couple of blog posts a few years ago about calling WCF from jQuery. This also covers fault handling - which many articles ignore.
Part one and Part two.
HTH
Iain
what i found in google, might help you.
$(document).ready(function() {
$("#sayHelloButton").click(function(event){
$.ajax({
type: "POST",
url: "dummyWebsevice.svc/HelloToYou",
data: "{'name': '" + $('#name').val() + "'}",
contentType: "application/json; charset=utf-8",
dataType: "json",
success: function(msg) {
AjaxSucceeded(msg);
},
error: AjaxFailed
});
});
});
function AjaxSucceeded(result) {
alert(result.d);
}
function AjaxFailed(result) {
alert(result.status + ' ' + result.statusText);
}
[WebMethod()]
public static string sayHello()
{
return "hello ";
}
I figured out how to finally call my simple WCF service from jQuery. I found some source code after reading this link:
http://www.west-wind.com/weblog/posts/2009/Sep/15/Making-jQuery-calls-to-WCFASMX-with-a-ServiceProxy-Client. This link doesn't provide the source but it was another page that referenced this link.
Anyway, I downloaded the code and started to compare my code versus this working project that was calling a WCF Service via jQuery. What I found was that my web.config file was way too complicated so I shorted that up considerably. Then I also realized that my methods were not public. So I made them public and then after a few more tweaks (i.e. taking out the namespaces) the page started to return the simple string I was trying to return.
<system.serviceModel>
<services>
<service name="pilltrakr" behaviorConfiguration="MyServiceTypeBehaviors">
<endpoint address="" behaviorConfiguration="pilltrakrAspNetAjaxBehavior" binding="webHttpBinding" contract="Ipilltrakr"/>
</service>
</services>
<behaviors>
<serviceBehaviors>
<behavior name="MyServiceTypeBehaviors">
<serviceMetadata httpGetEnabled="true"/>
<serviceDebug includeExceptionDetailInFaults="true"/>
</behavior>
</serviceBehaviors>
<endpointBehaviors>
<behavior name="pilltrakrAspNetAjaxBehavior">
<enableWebScript/>
</behavior>
</endpointBehaviors>
</behaviors>
<serviceHostingEnvironment aspNetCompatibilityEnabled="true"/>
</system.serviceModel>