2014-09-10 63 views
0

我需要創建一個WCF服務以從C#中的SQL數據庫中檢索數據,並使用JSON在HTML5中使用該WCF服務。在HTML頁面中調用WCF服務時出錯

我創建了WCF服務及其工作,但是當我嘗試在HTML中使用它時,它顯示「對象對象錯誤(無法加載資源:服務器響應的狀態爲415(無法處理消息,因爲內容類型'應用/ JSON的;字符集= UTF-8' 是不是預期的類型 '文本/ XML的,字符集= utf-8')」

幫我解決這個

的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> 
      <!-- To avoid disclosing metadata information, set the values below to false before deployment --> 
      <serviceMetadata httpGetEnabled="true"/> 
      <!-- To receive exception details in faults for debugging purposes, set the value below to true. Set to false before deployment to 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"/> 
    <!-- 
     To browse web app root directory during debugging, set the value below to true. 
     Set to false before deployment to 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) { 
        for (var i = 0; i < 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); 
} 

回答

0

WCF服務不指望JSON,但是,很可能,XML。您是否將服務配置爲接受POST請求和json數據?你可能會發現this question and answer有幫助。它描述瞭如何裝飾你想用WebInvokeAttribute調用的方法。

編輯:清理完代碼後,我看到你用GET裝飾了方法。如上所述,這應該是POST

+0

我也嘗試通過把POST而不是GET,仍顯示**對象對象錯誤無法加載資源:服務器響應狀態415(無法處理消息,因爲內容類型'application/json'是而不是期望的類型'text/xml; charset = utf-8'。)* 幫助我如何解決此問題 – Sathiya 2014-09-11 09:48:06