jquery - WCF restful service: Error the server responded with a status of 405 (Method Not Allowed) -
i know it's looks duplicate question.
here want call wcf rest service using jquery ajax , want pass country object have countryname parameter in rest service. whenever calling rest service using jquery ajax html page.but giving me error 405 method not allowed
.i tried lots of time solve it.but enable solve error.and trying pass data in json object.
iservice.cs:
[operationcontract] [webinvoke(uritemplate = "/addcountry", responseformat = webmessageformat.json,requestformat=webmessageformat.json)] string addcountry(tablecountry country);
service.cs
public string addcountry(tablecountry country) { //do code. }
web.config
<system.servicemodel> <bindings> <webhttpbinding> <binding name="webhttpbinding" crossdomainscriptaccessenabled="true"> <security mode="none"/> </binding> </webhttpbinding> </bindings> <behaviors> <servicebehaviors> <behavior name=""> <servicemetadata httpgetenabled="true"/> <servicedebug includeexceptiondetailinfaults="true"/> </behavior> </servicebehaviors> <endpointbehaviors> <behavior name="webbehaviour"> <webhttp/> </behavior> </endpointbehaviors> </behaviors> <services> <service name="service"> <endpoint address="rest" binding="webhttpbinding" contract="iservice" bindingconfiguration="webhttpbinding" behaviorconfiguration="webbehaviour"/> </service> </services> <servicehostingenvironment multiplesitebindingsenabled="true"/> </system.servicemodel> <system.webserver> <httpprotocol> <customheaders> <add name="access-control-allow-origin" value="*" /> </customheaders> </httpprotocol> <modules runallmanagedmodulesforallrequests="true"/> <directorybrowse enabled="true"/> </system.webserver>
ajax code
$("document").ready(function(){ var country = {"countryname":"iran"}; $.ajax({ type: "post", url: "http://localhost:2293/acfrestajaxparsing/service.svc/rest/addcountry", data: json.stringify({ country: country }), contenttype: "application/json; charset=utf-8", datatype: "jsonp", success: function(data){alert(data);}, failure: function(errmsg) { alert(errmsg); } }); });
i refer link solve error stackoverflow
if have idea error in case please me.
thanks in advance.
try this:
iservice.cs
[operationcontract] [webinvoke(uritemplate = "/addcountry", responseformat = webmessageformat.json,requestformat=webmessageformat.json)] string addcountry(tablecountry country); public class list_usercontact : list<tablecountry> { }
service.cs
public string addcountry(tablecountry country) { //do code. }
web.config
<system.servicemodel> <bindings> <webhttpbinding> <binding name="webhttpbinding" crossdomainscriptaccessenabled="true"> <security mode="none"/> </binding> </webhttpbinding> </bindings> <behaviors> <servicebehaviors> <behavior name=""> <servicemetadata httpgetenabled="true"/> <servicedebug includeexceptiondetailinfaults="true"/> </behavior> </servicebehaviors> <endpointbehaviors> <behavior name="webbehaviour"> <webhttp/> </behavior> </endpointbehaviors> </behaviors> <services> <service name="service"> <endpoint address="rest" binding="webhttpbinding" contract="iservice" bindingconfiguration="webhttpbinding" behaviorconfiguration="webbehaviour"/> </service> </services> <servicehostingenvironment multiplesitebindingsenabled="true"/> </system.servicemodel> <system.webserver> <httpprotocol> <customheaders> <add name="access-control-allow-origin" value="*" /> </customheaders> </httpprotocol> <modules runallmanagedmodulesforallrequests="true"/> <directorybrowse enabled="true"/> </system.webserver>
ajax code
function callwcf() { $.ajax({ type: "post", contenttype: "application/json; charset=utf-8", url: 'http://yourliveurl/acfrestajaxparsing/service.svc/rest/addcountry', data: '[{"countryid":2147483647,"countryname":"string content"}]', processdata: true, datatype: "json", success: servicesucceeded, error: servicefailed }); } function servicefailed(output) { log('service call failed: ' + output.status + ' ' + output.statustext); } function servicesucceeded(output) { var outputvalue = output; log("service call success: <br/> " + outputvalue); } function log(displayvaluefromservice) { $("#displayoutput").append('<br/>' + displayvaluefromservice); } </script>
run:
url:http://localhost:2293/acfrestajaxparsing/service.svc/rest/addcountry header content :application/json body:your json data
your service hosted in live ip
html file hosted in live ip
Comments
Post a Comment