【问题标题】:Error when calling WCF service in HTML page在 HTML 页面中调用 WCF 服务时出错
【发布时间】:2014-09-10 13:30:00
【问题描述】:

我需要创建一个 WCF 服务以使用 C# 从 SQL 数据库中检索数据,并使用 JSON 在 HTML5 中使用该 WCF 服务。

我创建了 WCF 服务及其工作,但是当我尝试在 HTML 中使用它时,它显示 “对象对象错误(加载资源失败:服务器响应状态为 415(无法处理消息,因为内容type 'application/json; charset=UTF-8' 不是预期的类型 'text/xml; charset=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>

HTML页面:

<!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);
}

【问题讨论】:

    标签: c# json wcf


    【解决方案1】:

    您的 WCF 服务不需要 json,但可能需要 xml。您是否将服务配置为接受 POST 请求和 json 数据?您可能会发现 this question and answer 很有帮助。它描述了如何用WebInvokeAttribute装饰你要调用的方法。

    编辑:清理代码后,我看到你用GET 修饰了方法。如前所述,这应该是POST

    【讨论】:

    • 我也尝试过使用 POST 而不是 GET,但仍然显示 *object Object error Failed to load resource: 服务器响应状态为 415(无法处理消息,因为内容类型 'application/json' 不是预期的类型 'text/xml; charset=utf-8'。) 帮我解决这个问题
    猜你喜欢
    • 1970-01-01
    • 2015-12-29
    • 2021-03-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-12-07
    • 2010-12-11
    相关资源
    最近更新 更多