Ajax POST调用将无法与WCF一起使用

时间:2016-02-24 19:24:29

标签: c# jquery json ajax wcf

我正在尝试通过Ajax POST调用向WCF服务发送数据 我用jSON发送数据

当我尝试拨打电话时,WCF服务无法获取发送的数据 调试显示我的输入参数等于null

这是我的源代码: jQuery方

$.ajax
    ({
        contentType: "application/json; charset=utf-8",
        dataType: "json",
        url: "http://192.168.0.12:25460/Service1.svc/getPost",
        type: 'POST',
        data: {"value": "test"},
        timeout: 5000,
        success: function (data, status, xhr)
        {
            alert('Success: '+data);
        },
        error: function(x, e)
        {
            alert(x.status + " " + x.responseText);
        }
    });

WCF方

Iservice1.cs

[ServiceContract]
public interface IService1
{
    [OperationContract]
    [WebInvoke(Method = "POST", ResponseFormat = WebMessageFormat.Json, BodyStyle=WebMessageBodyStyle.Wrapped, RequestFormat = WebMessageFormat.Json, UriTemplate = "/getPost?value={value}")]
    string getPost(string value);

    [OperationContract]
    CompositeType GetDataUsingDataContract(CompositeType composite);

}

Service1.svc

public class Service1 : IService1
{
    public string getPost(string value)
    {
        return "Reçu :" + value;
    }
}

的Web.config

    <?xml version="1.0" encoding="utf-8"?>
<configuration>

  <appSettings>
    <add key="aspnet:UseTaskFriendlySynchronizationContext" value="true" />
  </appSettings>
  <system.web>
    <compilation debug="true" targetFramework="4.5" />
    <httpRuntime targetFramework="4.5" maxUrlLength="500"/>
  </system.web>
  <system.serviceModel>
    <services>
        <service name="WcfService1.Service1">
            <!-- Service Endpoints -->
            <endpoint address="" binding="webHttpBinding" contract="WcfService1.IService1" behaviorConfiguration="webBehavior">
            </endpoint>
        </service>
    </services>
    <behaviors>
      <endpointBehaviors>
        <behavior name="webBehavior">
          <webHttp />
        </behavior>
      </endpointBehaviors>
      <serviceBehaviors>
        <behavior>
          <!-- Pour éviter la divulgation d'informations de métadonnées, définissez les valeurs ci-dessous sur false avant le déploiement -->
          <serviceMetadata httpGetEnabled="true" httpsGetEnabled="true"/>
          <!-- Pour recevoir les détails de l'exception dans les erreurs à des fins de débogage, définissez la valeur ci-dessous sur true. Pour éviter la divulgation d'informations d'exception, définissez-la sur false avant le déploiement -->
          <serviceDebug includeExceptionDetailInFaults="true"/>
        </behavior>
      </serviceBehaviors>
    </behaviors>
    <protocolMapping>
        <add binding="basicHttpsBinding" scheme="https" />
    </protocolMapping>    
    <serviceHostingEnvironment aspNetCompatibilityEnabled="true" multipleSiteBindingsEnabled="true" />
  </system.serviceModel>
  <system.webServer>
    <modules runAllManagedModulesForAllRequests="true"/>
    <!--
        Pour parcourir le répertoire racine de l'application Web lors du débogage, définissez la valeur ci-dessous sur true.
        Définissez-la sur false avant le déploiement pour ne pas divulguer d'informations du dossier de l'application Web.
      -->
    <directoryBrowse enabled="true"/>
  </system.webServer>

</configuration>

感谢您的帮助!

1 个答案:

答案 0 :(得分:0)

尝试以下更改。愿它能奏效..

jQuery方

$.ajax
    ({
        contentType: "application/json; charset=utf-8",
        dataType: "json",
        url: "http://192.168.0.12:25460/Service1.svc/getPost/test",//value in url..
        type: 'POST',
      //  data: {"value": "test"}, remove this line.
        timeout: 5000,
        success: function (data, status, xhr)
        {
            alert('Success: '+data);
        },
        error: function(x, e)
        {
            alert(x.status + " " + x.responseText);
        }
    });

WCF方

<强> Iservice1.cs

[ServiceContract]
public interface IService1
{
    [OperationContract]
    [WebInvoke(Method = "POST", ResponseFormat = WebMessageFormat.Json, BodyStyle=WebMessageBodyStyle.Wrapped, RequestFormat = WebMessageFormat.Json, UriTemplate = "/getPost/{value}")]//change here 
    string getPost(string value);

    [OperationContract]
    CompositeType GetDataUsingDataContract(CompositeType composite);
}

我只是更改参数发送方法并将其放入网址。