2014-09-10 4 views
0

C#으로 SQL 데이터베이스에서 데이터를 검색하고 JSON을 사용하여 HTML5에서 WCF 서비스를 사용하려면 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> 
012 3,516,

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 서비스는, 아마도, XML을 JSON을 기대하지만,하지 않습니다. POST 요청 및 json 데이터를 허용하도록 서비스를 구성 했습니까? this question and answer이 도움이 될 수 있습니다. WebInvokeAttribute을 사용하여 전화하려는 방법을 꾸미는 방법을 설명합니다.

편집 : 코드를 정리 한 후 GET으로 메서드를 장식했습니다. 설명한대로 POST이어야합니다.

+0

또한 GET 대신 POST를 사용하여 시도했지만, 여전히 ** object Object 오류를 표시했습니다. 리소스를로드하지 못했습니다. 서버가 415 상태로 응답했습니다 ('application/json'콘텐츠 유형이 있었기 때문에 메시지를 처리 ​​할 수 ​​없음). 예상되는 유형 'text/xml; charset = utf-8'이 아님) * 해결 방법 도움말 – Sathiya

관련 문제