我使用jquery从我的ASP.Net Web应用程序调用WCF服务来测试我的WCF服务。正在调用服务函数GetData,但参数值为null。我无法将数据传递给WCF服务。 我在调整内容类型方面尝试了不同的选项,没有成功。
WCF服务代码如下
namespace MapsSvc
{
//[AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Allowed)]
[ServiceBehavior(IncludeExceptionDetailInFaults = true)]
public class MapService : IMapService
{
//[OperationContract]
[WebInvoke(Method = "POST",
BodyStyle = WebMessageBodyStyle.Wrapped,
ResponseFormat = WebMessageFormat.Json,
RequestFormat = WebMessageFormat.Json)]
public string GetData(string value)
{
return string.Format("You entered: {0}", value);
}
}
}
接口文件代码如下
namespace MapsSvc
{
[ServiceContract]
public interface IMapService
{
[OperationContract]
string GetData(string value);
}
}
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"/>
<authentication mode="Windows" />
</system.web>
<system.serviceModel>
<behaviors>
<serviceBehaviors>
<behavior name="ServiceBehavior">
<!-- To avoid disclosing metadata information, set the values below to false before deployment -->
<serviceMetadata httpGetEnabled="true" httpsGetEnabled="true"/>
<!-- To receive exception details in faults for debugging purposes, set the value below to true. Set to false before deployment to avoid disclosing exception information -->
<serviceDebug includeExceptionDetailInFaults="true"/>
</behavior>
</serviceBehaviors>
<endpointBehaviors>
<behavior name="EndpBehavior">
<webHttp />
</behavior>
</endpointBehaviors>
</behaviors>
<services>
<!--<service name="AJAXEnabledWebService.Service">
<endpoint address="" behaviorConfiguration="AJAXEnabledWebService.ServiceAspNetAjaxBehavior"
binding="webHttpBinding" contract="AJAXEnabledWebService.IService" />
</service>-->
<service behaviorConfiguration="ServiceBehavior" name="MapsSvc.MapService">
<endpoint address="" binding="webHttpBinding" contract="MapsSvc.IMapService" behaviorConfiguration="EndpBehavior"/>
</service>
</services>
<protocolMapping>
<add binding="basicHttpsBinding" scheme="https" />
</protocolMapping>
<serviceHostingEnvironment aspNetCompatibilityEnabled="true" multipleSiteBindingsEnabled="true" />
</system.serviceModel>
<system.webServer>
<modules runAllManagedModulesForAllRequests="true"/>
<directoryBrowse enabled="true"/>
</system.webServer>
</configuration>
将JSON数据发送到WCF服务的html文件如下
<!DOCTYPE html>
<html>
<head>
<title>Call WCF</title>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.11.2/jquery.min.js"></script>
<script type="text/javascript">
var counter = 0;
$.support.cors = true;
function CallMyService() {
counter++;
counter = "Nate";
$.ajax({
type: "POST",
url: "http://localhost:63739/MapService.svc/GetData",
data: '{"Count": "' + counter + '"}',
contentType: "application/json",
success: ServiceSucceeded,
error: ServiceFailed
});
}
// ---- WCF Service call backs -------------------
function ServiceFailed(result) {
Log('Service call failed: ' + result.status + ' ' + result.statusText);
}
function ServiceSucceeded(result) {
var resultObject = result.MyFunctionResult;
Log("Success: " + resultObject);
}
// ---- Log ----------------------------------------
function Log(msg) {
$("#logdiv").append(msg + "<br />");
}
</script>
</head>
<body>
<input id="Button1" type="button" value="Execute" onclick="CallMyService();" />
<div id="logdiv"></div> <!--For messages-->
</body>
</html>
答案 0 :(得分:0)
.ajax({
type: "POST",
url: "http://localhost:63739/MapService.svc/GetData",
data: '{"value": "' + counter + '"}',
contentType: "application/json",
应该是有价值的。谢谢Lukas