jQuery - 使用 ajax 调用将 JSON 发送到 WCF 服务为空
Posted
技术标签:
【中文标题】jQuery - 使用 ajax 调用将 JSON 发送到 WCF 服务为空【英文标题】:jQuery - Sending JSON to WCF Service with ajax call is null 【发布时间】:2017-03-17 00:46:13 【问题描述】:我刚刚开始学习 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
);
通过合约传递:
[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.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>
问题是在进行调用并且 Web 服务开始处理调用后,userID 值为 null。
首先,当 JSON 传递到合同中时,我不确定我的服务期望什么作为输入。我还怀疑我将数据传递到网络合同的方式无法正确处理(使用UriTemplate
)。它似乎也只打了第二份合同:
[OperationContract]
[WebInvoke(Method = "OPTIONS", UriTemplate = "*",
RequestFormat = WebMessageFormat.Json,
ResponseFormat = WebMessageFormat.Json)]
void CallDateSetupOptions(string userID);
目前 WCF 服务和调用该服务的 Web 应用程序是分开的,我需要在其他 web.config 中进行任何其他潜在配置吗?也许在<connectionStrings>
?
任何帮助将不胜感激,一直坚持这一点。
【问题讨论】:
【参考方案1】: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"
【讨论】:
感谢您的回答 :) 虽然在指定要发布的 UriTemplate 或选项时,根据数据传递的方法,它会返回 404。网络中是否有问题。在那种情况下配置? 否则 UriTemplate 为 "*" 它仍然返回 null以上是关于jQuery - 使用 ajax 调用将 JSON 发送到 WCF 服务为空的主要内容,如果未能解决你的问题,请参考以下文章
如何使用 Json 数据作为参数进行 Jquery Ajax 调用?
Jquery DataTable 将参数传递给ajax 调用asp.net。无效的 JSON 原语
使用 JSON 从 AJAX 和 JQuery 调用简单的 Web 服务(.asmx 文件) - 解析错误