Cannot call Webmethod from JQuery
Question
I cannot call webmethod from jquery. I think its about web.config file. How can i set web.config file for web services and webmethod?
Solution
Do you have this code?
$.ajax({
url: "Services/MyService.svc/Service",
type: "GET",
context: document.body,
contentType: 'application/json; charset=utf-8',
datatype: 'json',
success: function (data) {
// do something
}
});
note that contentType part is critical.
If you do, check Firebug for exact error that gets thrown in the "Net" tab. Normally, people have different problems depending on service type - ASP.NET asmx vs. WCF svc. For asmx configuration, refer to How to let an ASMX file output JSON . For wcf, you need to set up web.config to allow web scripting, like so:
<system.serviceModel>
<behaviors>
<endpointBehaviors>
<behavior name="AspNetAjaxBehavior">
<enableWebScript/>
</behavior>
</endpointBehaviors>
</behaviors>
and then later configure the service to use that behavior:
<services>
<service name="MyProject.Services.MyService">
<endpoint address="/Services/MyService.svc" behaviorConfiguration="AspNetAjaxBehavior" binding="webHttpBinding" contract="MyProject.Services.MyService"/>
</service>
</services>
</system.serviceModel>
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow