简体   繁体   English

jQuery-使用Ajax调用将JSON发送到WCF服务为空

[英]jQuery - Sending JSON to WCF Service with ajax call is null

I'm just starting in learning about WCF Services and making an ajax call to the Service using jQuery: 我只是开始学习WCF服务,并使用jQuery对服务进行ajax调用:

function CallService() {
    $.ajax({
        type: "POST",
        url: "http://localhost:18091/MainframeDateChange.svc/CallDateSetup",
        data: JSON.stringify({
        "userID": userID,
        "password": password,
        "environment": environment,
        "newDate": newDate,
        "newTime": newTime
    });,
        contentType: "application/json; charset=utf-8",
        dataType: "json",
        success: function (msg) {
            ServiceSucceeded(msg);
        },
        error: ServiceFailed
    });
}

Which is passed through the contract: 通过合同传递的:

[ServiceContract]
public interface IMainframeDateChange
{

    [OperationContract]
    [WebInvoke(Method = "POST",
     BodyStyle = WebMessageBodyStyle.Wrapped,
     ResponseFormat = WebMessageFormat.Json,
     RequestFormat = WebMessageFormat.Json)]
    void CallDateSetup(string userID);
    //, string password, string envName, string newDate, string newTime

    [OperationContract]
    [WebInvoke(Method = "OPTIONS", UriTemplate = "*",
        RequestFormat = WebMessageFormat.Json, 
        ResponseFormat = WebMessageFormat.Json)]
    void CallDateSetupOptions(string userID);
}

Here is my service Web.config 这是我的服务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 name="ServiceBehavior">
          <serviceMetadata httpGetEnabled="true"/>
          <serviceDebug includeExceptionDetailInFaults="true"/>
        </behavior>
        <behavior>
          <serviceMetadata httpGetEnabled="true" httpsGetEnabled="true"/>
          <serviceDebug includeExceptionDetailInFaults="false"/>
        </behavior>
      </serviceBehaviors>
      <endpointBehaviors>
        <behavior name="EndpBehavior">
          <webHttp/>
        </behavior>
      </endpointBehaviors>
    </behaviors>
    <services>
      <service behaviorConfiguration="ServiceBehavior" name="MainframeServiceHandler.MainframeDateChange">
        <endpoint address="" binding="webHttpBinding"
            contract="MainframeServiceHandler.IMainframeDateChange" behaviorConfiguration="EndpBehavior"/>
      </service>
    </services>
    <protocolMapping>
      <add binding="basicHttpsBinding" scheme="https" />
    </protocolMapping>
    <serviceHostingEnvironment>
      <serviceActivations>
        <add factory="System.ServiceModel.Activation.WebServiceHostFactory"
             relativeAddress="./MainframeServiceHandler/MainframeDateChange.svc"
             service="MainframeServiceHandler.MainframeDateChange" />
      </serviceActivations>
    </serviceHostingEnvironment>
  </system.serviceModel>
  <system.webServer>
    <modules runAllManagedModulesForAllRequests="true"/>
    <httpProtocol>
      <customHeaders>
        <add name="Access-Control-Allow-Origin" value="*" />
        <add name="Access-Control-Allow-Headers" value="Origin, X-Requested-With, Content-Type, Accept,Authorization" />
        <add name="Access-Control-Allow-Methods" value="GET, POST, PUT, DELETE, OPTIONS" />
      </customHeaders>
    </httpProtocol>
    <directoryBrowse enabled="true"/>
  </system.webServer>

</configuration>

The issue is that after the call is made and the web service starts handling the call, the userID value is null. 问题在于,在进行呼叫并且Web服务开始处理呼叫之后,userID值为null。

Firstly I am unsure as to what my service is to be expecting as input when the JSON is passed into the contract. 首先,我不确定将JSON传递到合同中时期望我的服务作为输入。 I also suspect the way I am passing through the data into the web contract could not be properly handled (with the UriTemplate ). 我还怀疑无法正确处理将数据传递到Web合同的方式(使用UriTemplate )。 It also seems to only hit the second contract: 似乎也只能达成第二份合同:

[OperationContract]
[WebInvoke(Method = "OPTIONS", UriTemplate = "*",
    RequestFormat = WebMessageFormat.Json, 
    ResponseFormat = WebMessageFormat.Json)]
void CallDateSetupOptions(string userID);

Currently the WCF service and the web application calling the service are separate, is there any other potential configuration I need to make in the other web.config? 当前,WCF服务和调用该服务的Web应用程序是分开的,是否需要在其他web.config中进行其他配置? Perhaps in <connectionStrings> ? 也许在<connectionStrings>

Any help would be greatly appreciated, been stuck on this one a while. 任何帮助将不胜感激,卡在这一段时间。

A UriTemplate uniquely identifies an action on the service.You should do it like this - UriTemplate唯一地标识对服务的操作。您应该这样做-

[ServiceContract]
public interface IService1
{

    [OperationContract]
    [WebInvoke(Method = "POST", UriTemplate = "post",
 BodyStyle = WebMessageBodyStyle.Wrapped,
 ResponseFormat = WebMessageFormat.Json,
 RequestFormat = WebMessageFormat.Json)]
    bool CallDateSetup(CompositeType data);

where - 哪里-

[DataContract]
public class CompositeType
{
    [DataMember]
    public string userID { get; set; }
    [DataMember]
    public string password { get; set; }
    [DataMember]
    public string envName { get; set; }
    [DataMember]
    public string newDate { get; set; }
    [DataMember]
    public string newTime { get; set; }
}

sample request - 样品要求-

{
   "data": {
   "userID": "uiag61",
   "password": "password",
   "envName": "environment",
   "newDate": "newDate",
   "newTime": "newTime"
  }
}

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

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