jQuery - 使用ajax调用将JSON发送到WCF服务为null

时间:2016-11-03 12:23:37

标签: c# jquery json ajax wcf

我刚开始学习WCF服务并使用jQuery对服务进行ajax调用:

def new
    if is_path?("/payments/new?subcription_id=1&user=4")
      @subscription = Subscription.find(params[:subcription_id])
      @subcription_id = params[:subcription_id]
    elsif is_path?("/payments/new?event_id=2&user=4")
      @event = Event.find(params[:event_id])
      @event_id = params[:event_id]
    end
    @payment = Payment.new
  end

def is_path?(*paths)
    paths.include?(request.path)
  end

通过合同传递的内容:

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

这是我的服务Web.config

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

问题是在进行调用并且Web服务开始处理调用之后,userID值为null。

首先,我不确定当JSON传递给合同时我的服务是什么作为输入。我还怀疑我将数据传递到Web合同的方式无法正确处理(使用<?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> )。它似乎也只达到了第二份合同:

UriTemplate

目前,WCF服务和调用该服务的Web应用程序是分开的,是否还需要在其他web.config中进行其他任何可能的配置?也许在[OperationContract] [WebInvoke(Method = "OPTIONS", UriTemplate = "*", RequestFormat = WebMessageFormat.Json, ResponseFormat = WebMessageFormat.Json)] void CallDateSetupOptions(string userID);

任何帮助都会非常感激,一段时间都被困在这个。

1 个答案:

答案 0 :(得分:0)

UriTemplate唯一标识服务上的操作。您应该这样做 -

[ServiceContract]
public interface IService1
{

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

其中 -

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

样品申请 -

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