【问题标题】:Ajax POST call won't working with WCFAjax POST 调用不适用于 WCF
【发布时间】:2016-06-07 06:37:57
【问题描述】:

我正在尝试通过 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>

感谢您的帮助!

【问题讨论】:

  • 这对我来说看起来很可疑/getPost?value={value} 这样的东西将用于 GET 而不是 POST。除掉 ?以及之后的一切。

标签: c# jquery json ajax wcf


【解决方案1】:

尝试以下更改。愿它行得通..

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

我只是改变了你的参数发送方式,并把它放在 URL 中。

【讨论】:

    猜你喜欢
    • 2011-04-04
    • 2020-06-30
    • 2016-05-26
    • 1970-01-01
    • 1970-01-01
    • 2012-03-05
    • 1970-01-01
    • 2011-07-08
    • 1970-01-01
    相关资源
    最近更新 更多