2016-07-22 94 views
0

Service.cs客戶端代碼使用WCF

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; charset=utf-8' was not the expected type '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> 

標記:

<!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> 

接口:

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

WebCo nfig:

<?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> 
+0

@RajaprabhuAravindasamy你認爲添加兩個冒號使它更具可讀性嗎? – CodeCaster

+0

你的服務只有一個basicHttpBinding,這意味着你只能用SOAP1.1來解決它。如果你想爲它發佈一些JSON,你還需要添加一個webHttpBinding。 – CodeCaster

+0

@CodeCaster當然可以。做這樣的修改是犯罪嗎? –

回答

-1

默認情況下,所有WCF通信都使用XML進行。 試圖糾正你的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); 
      } 
     }); 
+0

也試過這個,但得到錯誤400 –

-1

變化Ajax調用contentType: 'text/xml; charset=utf-8'內容類型它應該工作的罰款。

如果您收到錯誤400,請嘗試從瀏覽器訪問URL並更正URL。