简体   繁体   English

使用WCF的客户端代码

[英]Client Side Code Using WCF

Service.cs Service.cs

public class StoredProcService : IStoredProcService
{

    public int addData(int x, int y)
    {

        return x + y;
    }
}

Error:415 Cannot process the message because the content type 'application/json; 错误:415无法处理消息,因为内容类型为'application / json;。 charset=utf-8' was not the expected type 'text/xml'aspx charset = utf-8'不是预期的类型'text / xml'aspx

<script type="text/javascript">
  $(document).ready(function () {
      //$('input[id^="button"]').click(function () {
      //                alert('You have clicked ' + $(this).val());
      Add();
  })

  function Add() {
      alert("sds");
      $.ajax({
          type: 'POST',
          url: '/StoredProcService.svc/addData',
          contentType: 'application/json; charset=utf-8',
          dataType: 'json',
          data: {"x":"5", "y":"8"},
          success: function (data) {
              alert(data);
          }
      });
  }
</script>

Markup: 标记:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
    <script type="text/javascript" src="http://code.jquery.com/jquery-latest.min.js" charset="utf-8"></script>
</head>

<body>
    <form id="form1" runat="server">
    <div>
        <asp:Button ID="Button1" runat="server" Text="Server Code" onclick="Button1_Click" />
    &nbsp;&nbsp;&nbsp;&nbsp;
        <input id="button" type="button" value="Client Side" onclick="Add()" />        
    </div>
    </form>
</body>
</html>

Interface: 接口:

[ServiceContract]
public interface IStoredProcService
{

    [OperationContract]
    [WebInvoke(Method = "POST", 
               RequestFormat = WebMessageFormat.Json, 
               ResponseFormat = WebMessageFormat.Json, 
               BodyStyle = WebMessageBodyStyle.WrappedRequest, 
               UriTemplate = "addData")]
    int addData(int x, int y);
}

WebConfig: WebConfig:

<?xml version="1.0"?>
<!--
  For more information on how to configure your ASP.NET application, please visit
  http://go.microsoft.com/fwlink/?LinkId=169433
  -->
<configuration>
  <system.web>
    <compilation debug="true" targetFramework="4.5" />
    <httpRuntime targetFramework="4.5" />
  </system.web>
  <appSettings>`
    <add key="vs:EnableBrowserLink" value="false"/>
    <add key="erpConnectionString" value="Data Source=mssql.aksharasoftware.com,1437;Initial Catalog=erp;uid=erp;password=100_erp;Pooling=true;Connection Lifetime=0;Min Pool Size=2;Max Pool Size=400;Connection Timeout=1200;"/>
    <add key="PINEDITLICENSEKEY" value="141607143E07144807141606144804144762143E05142A06135162"/>
    <add key="owin:AutomaticAppStartup" value="false"/>
  </appSettings>
  <system.serviceModel>
    <bindings>
      <basicHttpBinding>
        <binding name="BasicHttpBinding_IService" />
        <binding name="BasicHttpBinding_ISamplereturn" />
        <binding name="BasicHttpBinding_IStoredProcService" />
      </basicHttpBinding>
    </bindings>
    <client>
      <endpoint address="http://localhost:50192/Service.svc"
                binding="basicHttpBinding"
                bindingConfiguration="BasicHttpBinding_IService"
                contract="ServiceReference1.IService"
                name="BasicHttpBinding_IService" />
      <endpoint address="http://localhost:50192/Samplereturn.svc"
                binding="basicHttpBinding"
                bindingConfiguration="BasicHttpBinding_ISamplereturn"
                contract="ServiceReference2.ISamplereturn"
                name="BasicHttpBinding_ISamplereturn" />
      <endpoint address="http://localhost:50192/StoredProcService.svc"
                binding="basicHttpBinding"
                bindingConfiguration="BasicHttpBinding_IStoredProcService"
                contract="ServiceReference3.IStoredProcService"
                name="BasicHttpBinding_IStoredProcService" />
    </client>
    <behaviors>
      <serviceBehaviors>
        <behavior name="">
          <serviceMetadata httpGetEnabled="true" httpsGetEnabled="true" />
          <serviceDebug includeExceptionDetailInFaults="false" />
        </behavior>
      </serviceBehaviors>
    </behaviors>
    <serviceHostingEnvironment aspNetCompatibilityEnabled="true"
                               multipleSiteBindingsEnabled="true" >
      <serviceActivations>
        <add factory="System.ServiceModel.Activation.WebServiceHostFactory"
             relativeAddress="./ServiceReference3/StoredProcService.svc"
             service="StoredProcService"/>
      </serviceActivations>
    </serviceHostingEnvironment>
  </system.serviceModel>
</configuration>

By default all the WCF communication occurs using XML. 默认情况下,所有WCF通信都使用XML进行。 Try to correct your javascript: 尝试更正您的JavaScript:

function Add() {
        alert("sds");
        $.ajax({
            type: 'POST',
            url: '/StoredProcService.svc/addData',
            contentType: 'text/xml; charset=utf-8',
            dataType: 'xml',
            data: {"x":"5", "y":"8"},
            success: function (data) {
                alert(data);
            }
        });

Change the content type in ajax call to contentType: 'text/xml; charset=utf-8' 将ajax调用中的内容类型更改为contentType: 'text/xml; charset=utf-8' contentType: 'text/xml; charset=utf-8' it should work fine. contentType: 'text/xml; charset=utf-8'应该可以正常工作。

If you are getting error 400 then try to access URL from browser and correct the URL. 如果出现错误400,请尝试从浏览器访问URL并更正URL。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM