从 jquery ajax 调用 wcf 服务 - 出现异常 - 不允许传入请求
calling wcf service from jquery ajax - getting exception - incoming request is not allowed
异常详情:
A first chance exception of type 'System.InvalidOperationException' occurred in System.ServiceModel.Web.dll
Additional information: The HTTP method 'GET' of the incoming request (with URI http://localhost:24342/Test.svc/DoWork?callback=jQuery110203769165082986856_1479380612393&testParameter=TEST&_=1479380612394) is not allowed.
我正在使用 Ajax 嵌入式 WCF 服务。 WCF 客户端是一个 jQuery Ajax 调用。我的代码:
[ServiceContract(Namespace = "testWCFAjax")]
[AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Allowed)]
public class Service
{
[OperationContract]
[System.ServiceModel.Web.WebInvoke(Method = "POST", BodyStyle = WebMessageBodyStyle.WrappedRequest,
RequestFormat = WebMessageFormat.Json, ResponseFormat = WebMessageFormat.Json )]
public string DoWork(string testParameter)
{
return "success";
}
}
网络配置:
<system.serviceModel>
<behaviors>
<endpointBehaviors>
<behavior name="ServiceAspNetAjaxBehavior">
<enableWebScript/>
</behavior>
</endpointBehaviors>
</behaviors>
<serviceHostingEnvironment aspNetCompatibilityEnabled="true" multipleSiteBindingsEnabled="true"/>
<services>
<service name="Service">
<endpoint address="" behaviorConfiguration="ServiceAspNetAjaxBehavior" binding="webHttpBinding" contract="Service"/>
</service>
</services>
</system.serviceModel>
Ajax代码:
<script src="Scripts/jquery-1.9.2.min.js" type="text/javascript"></script>
<script type="text/javascript">
function CallMyService() {
$.support.cors = true;
var param = '{"testParameter": "TEST"}';
$.ajax({
url: 'http://localhost:24342/Test.svc/DoWork',
data: param,
type: "POST",
dataType: "jsonp",
contentType: "application/json",
success: ServiceSucceeded,
error: ServiceFailed
});
}
我能够通过使用 Ajax-embedded 创建 WCF 服务来解决这个问题。
异常详情:
A first chance exception of type 'System.InvalidOperationException' occurred in System.ServiceModel.Web.dll Additional information: The HTTP method 'GET' of the incoming request (with URI http://localhost:24342/Test.svc/DoWork?callback=jQuery110203769165082986856_1479380612393&testParameter=TEST&_=1479380612394) is not allowed.
我正在使用 Ajax 嵌入式 WCF 服务。 WCF 客户端是一个 jQuery Ajax 调用。我的代码:
[ServiceContract(Namespace = "testWCFAjax")]
[AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Allowed)]
public class Service
{
[OperationContract]
[System.ServiceModel.Web.WebInvoke(Method = "POST", BodyStyle = WebMessageBodyStyle.WrappedRequest,
RequestFormat = WebMessageFormat.Json, ResponseFormat = WebMessageFormat.Json )]
public string DoWork(string testParameter)
{
return "success";
}
}
网络配置:
<system.serviceModel>
<behaviors>
<endpointBehaviors>
<behavior name="ServiceAspNetAjaxBehavior">
<enableWebScript/>
</behavior>
</endpointBehaviors>
</behaviors>
<serviceHostingEnvironment aspNetCompatibilityEnabled="true" multipleSiteBindingsEnabled="true"/>
<services>
<service name="Service">
<endpoint address="" behaviorConfiguration="ServiceAspNetAjaxBehavior" binding="webHttpBinding" contract="Service"/>
</service>
</services>
</system.serviceModel>
Ajax代码:
<script src="Scripts/jquery-1.9.2.min.js" type="text/javascript"></script>
<script type="text/javascript">
function CallMyService() {
$.support.cors = true;
var param = '{"testParameter": "TEST"}';
$.ajax({
url: 'http://localhost:24342/Test.svc/DoWork',
data: param,
type: "POST",
dataType: "jsonp",
contentType: "application/json",
success: ServiceSucceeded,
error: ServiceFailed
});
}
我能够通过使用 Ajax-embedded 创建 WCF 服务来解决这个问题。