c# - Error when calling WCF service in HTML page -
i need create wcf service retrieve data sql database in c# , consume wcf service in html5 using json.
i created wcf service , working when tried consume in html shows "object object error(failed load resource: server responded status of 415 (cannot process message because content type 'application/json; charset=utf-8' not expected type 'text/xml; charset=utf-8'.)"
help me resolve this
web.config:
<?xml version="1.0"?> <configuration> <appsettings> <add key="aspnet:usetaskfriendlysynchronizationcontext" value="true" /> </appsettings> <system.web> <compilation debug="true" targetframework="4.5" /> <httpruntime targetframework="4.5"/> </system.web> <system.servicemodel> <behaviors> <servicebehaviors> <behavior> <!-- avoid disclosing metadata information, set values below false before deployment --> <servicemetadata httpgetenabled="true"/> <!-- receive exception details in faults debugging purposes, set value below true. set false before deployment avoid disclosing exception information --> <servicedebug includeexceptiondetailinfaults="false"/> </behavior> </servicebehaviors> </behaviors> <protocolmapping> <add binding="basichttpsbinding" scheme="https" /> </protocolmapping> <servicehostingenvironment aspnetcompatibilityenabled="true"/> <services> <service name="taskk2.service1" behaviorconfiguration=""> <endpoint address="" binding="basichttpbinding" behaviorconfiguration="" contract="taskk2.iservice1"/> <endpoint address="mex" binding="mexhttpbinding" contract="imetadataexchange"/> </service> </services> </system.servicemodel> <system.webserver> <modules runallmanagedmodulesforallrequests="true"/> <!-- browse web app root directory during debugging, set value below true. set false before deployment avoid disclosing web app folder information. --> <directorybrowse enabled="true"/> </system.webserver> </configuration>
htmlpage:
<!doctype html> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <title></title> <script type="text/javascript" src="http://code.jquery.com/jquery-1.8.2.js"></script> <script type="text/javascript"> $(function () { //$('#tbdetails').hide(); $('#tbdetails').show(); $('#btnclick').click(function () { $.ajax({ type: "post", contenttype: "application/json; charset=utf-8", url: 'service1.svc/getemployeedetails', data: '{"emp_id": "' + $("#txtname").val() + '"}', datatype: "json", processdata: false, success: function (data) { (var = 0; < data.d.length; i++) { $("#tbdetails").append("<tr><td>" + data.d[i].emp_id + "</td><td>" + data.d[i].emp_name + "</td><td>" + data.d[i].emp_age + "</td><td>" + data.d[i].emp_department + "</td><td>" + data.d[i].emp_salary + "</td></tr>"); } }, error: function (result) { alert(result); } }); }); }); </script> <style type="text/css"> table,th,td { border:1px solid black; border-collapse:collapse; } </style> </head> <body> <form id="form1" runat="server"> <b>enter employeeid:</b> <input type="text" id="txtname" /> <input type ="button" id="btnclick" value="get data" /> <table id="tbdetails"> <thead style="background-color:#dc5807; color:white; font-weight:bold"> <tr style="border:solid 1px #000000"> <td>emp_id</td> <td>emp_name</td> <td>emp_age</td> <td>emp_department </td> <td>emp_salary</td> </tr> </thead> <tbody> </tbody> </table> </form> </body> </html>
iservice.cs:
[servicecontract] public interface iservice1 { [operationcontract] [webinvoke(method = "get", uritemplate = "/getemployeedetails/{emp_id}", responseformat = webmessageformat.json, requestformat = webmessageformat.json, bodystyle = webmessagebodystyle.wrapped)] employeedetails[] getemployeedetails(string emp_id); }
your wcf service not expect json but, probably, xml. did configure service accept post
requests , json data? might find this question , answer helpful. describes how decorate method want call webinvokeattribute
.
edit: after cleaning code saw decorated method get
. said, should post
.
Comments
Post a Comment