404 Not Found error while calling WCF service from Jquery ajax -
i trying access wcf service hosted on iis html page using jquery ajax call,i not able hit service , throwing 404 not found error, can please know should make changes in jquery ajax call or web config file access service hosted in iis or other remote machine
html page:
<!doctype html> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <script src="http://code.jquery.com/jquery-latest.js"></script> <script src="json.js"></script> <title></title> <script type="text/javascript"> var inputdata = { "userid": "101"}; jquery.support.cors = true; $.ajax({ url: 'http://<ipaddress>/wcfservice1/service1.svc/getuserdetails', data: json.stringify(inputdata), type: 'post', datatype : "jsonp", contenttype: "application/json; charset=utf-8", //jsonpcallback: "handleresponse", success: function (result) { console.log(result.data); alert("success"); }, error: function (request, error) { alert('network error has occurred please try again.please check connection , try again.'); return;} }); </script> </head> <body> </body> </html> wcf service :
namespace wcfservice1 { [servicebehavior(addressfiltermode = addressfiltermode.any)] public class service1 : iservice1 { public string getuserdetails(string userid) { // returns user details } } } interface : iservice1.cs
namespace wcfservice1 { [servicecontract] public interface iservice1 { [operationcontract] [webinvoke(method = "post", responseformat = webmessageformat.json)] int getuserdetails(string struserid); } } webconfig file:
<?xml version="1.0"?> <configuration> <system.web> <compilation debug="true" targetframework="4.5" /> <httpruntime targetframework="4.5"/> </system.web> <system.servicemodel> <behaviors> <servicebehaviors> <behavior name="servicebehavior"> <servicemetadata httpgetenabled="true"/> <servicedebug includeexceptiondetailinfaults="true"/> </behavior> </servicebehaviors> <endpointbehaviors> <behavior name="endpbehavior"> <enablewebscript/> </behavior> </endpointbehaviors> </behaviors> <services> <service behaviorconfiguration="servicebehavior" name="wcfservice1.service1"> <endpoint address="" binding="webhttpbinding" contract="wcfservice1.iservice1" behaviorconfiguration="endpbehavior" bindingconfiguration="crossdomain"/> <host> <baseaddresses> <add baseaddress="http://localhost/wcfservice1/service1.svc"/> </baseaddresses> </host> </service> </services> <!--<protocolmapping> <add binding="basichttpsbinding" scheme="https" /> </protocolmapping>--> <servicehostingenvironment aspnetcompatibilityenabled="true" multiplesitebindingsenabled="true" > </servicehostingenvironment> <bindings> <webhttpbinding> <binding name="crossdomain" crossdomainscriptaccessenabled="true"/> </webhttpbinding> </bindings> </system.servicemodel> <system.webserver> <modules runallmanagedmodulesforallrequests="true"/> <directorybrowse enabled="true"/> <httpprotocol> <customheaders> <add name="access-control-allow-origin" value="*" /> <add name="access-control-allow-headers" value="content-type, accept" /> <add name="access-control-allow-methods" value="post,get,options" /> <add name="access-control-max-age" value="1728000" /> </customheaders> </httpprotocol> </system.webserver> </configuration>
here way go around:
[operationcontract] [webget(uritemplate = "/getuserdetails/{struserid}", responseformat = webmessageformat.json)] user getuserdetails(string struserid); and user custom class holding detail.
[datacontract] public class user { [datamemeber] public in userid {get; set;} // remaining attributes here. } i don't have experience ajax think should work.
url: 'http://<ipaddress>/wcfservice1/service1.svc/getuserdetails/101' type: 'get', //contenttype: "application/json; charset=utf-8", no need mention
Comments
Post a Comment