jQuery获取失败,没有错误消息
我正在对WCF web服务发出jQuery获取失败,没有错误消息,jquery,wcf,http-get,Jquery,Wcf,Http Get,我正在对WCF web服务发出GET请求。我的WCF服务位于http://localhost/RestService/RestService.svc/web/GetMessage并具有以下界面: [OperationContract] [WebGet(UriTemplate = "GetMessage", ResponseFormat = WebMessageFormat.Json)] String GetMessage(); 端点配置正确,因为我可以在浏览器中进行裸调用: <sys
GET
请求。我的WCF服务位于http://localhost/RestService/RestService.svc/web/GetMessage
并具有以下界面:
[OperationContract]
[WebGet(UriTemplate = "GetMessage", ResponseFormat = WebMessageFormat.Json)]
String GetMessage();
端点配置正确,因为我可以在浏览器中进行裸调用:
<system.serviceModel>
<behaviors>
<serviceBehaviors>
<behavior name="WebServiceBehavior">
<serviceMetadata httpGetEnabled="true" />
<serviceDebug includeExceptionDetailInFaults="true"/>
</behavior>
</serviceBehaviors>
<endpointBehaviors>
<behavior name="WebEndpointBehavior">
<webHttp />
</behavior>
</endpointBehaviors>
</behaviors>
<services>
<service name="TestRestService.RestService"
behaviorConfiguration="WebServiceBehavior">
<endpoint name="RestWeb"
address="web"
binding="webHttpBinding"
behaviorConfiguration="WebEndpointBehavior"
contract="TestRestService.IRestService" />
</service>
</services>
</system.serviceModel>
到目前为止还不错。这里没问题。快速查看用于执行GET
的
<html>
<head>
<script type="text/javascript" src="jquery-1.7.2.min.js"></script>
<script type="text/javascript">
$.ajax({
url: 'http://localhost/RestService/RestService.svc/web/GetMessage',
type: 'GET',
dataType: 'json',
success: function (data) {
alert(data);
},
error: function (xhr, status, message) {
alert("Error: " + status + " " + message); }
});
</script>
</head>
<body>
</body>
</html>
有什么好处?错误消息处理程序提供的有用信息绝对为零。简言之:
- 为什么我的GET失败了
- 为什么错误消息是无用的
dataType:'jsonp'
,并添加webHttpBinding
,启用crossDomainScriptEnabled
属性:
<bindings>
<webHttpBinding>
<binding name="WebBindingWithScripts"
crossDomainScriptAccessEnabled="true">
<security mode="None" />
</binding>
</webHttpBinding>
</bindings>
<endpoint name="RestWeb"
address="web"
binding="webHttpBinding"
behaviorConfiguration="WebEndpointBEhavior"
bindingConfiguration="WebBindingWithScripts"
contract="TestService.IRestService">
</endpoint>
当仅使用数据类型:“jsonp”时,除非您将WCF服务配置为允许跨域脚本,否则仍然会出现错误。您是否正在发出跨域请求?我看到您正在使用localhost,但这并不一定意味着您正在从localhost请求(据我们所知,您可能正在使用不同的端口或协议) 在这种情况下,请求失败的原因有两个:
我希望它是#1,因为您没有在控制台中看到
同源
错误。但是,由于您得到的是error
而不是parseerror
,所以这不太可能。我曾经遇到过这种情况,我所做的只是在我的web.config中添加EndpointBehavior来支持像这样的WebHttp请求
<behaviors>
<serviceBehaviors>
......
</behavior>
</serviceBehaviors>
<endpointBehaviors>
<behavior name="EndpBehavior">
<webHttp/>
</behavior>
</endpointBehaviors>
尝试使用浏览器的devtool(对于chrome ctrl+shift+J)。你能看到请求在进行吗?控制台上有错误吗?请尝试删除
数据类型
,然后让jquery推断出来。您还可以尝试设置数据类型:“jsonp”
。您确定web服务返回的是MIME类型的json/应用程序吗?@JohnKalberer:删除数据类型
仍然会出现相同的错误@ianpgall:我已经在问题中添加了服务的签名@我会调查的。@strustmaster:firefox13.0.1的错误控制台和开发控制台没有显示错误消息。我正在发出一个本地到本地的请求。页面和服务完全在同一页面上。事实上,页面本身只是一个简单的html,上面有我发布的jQuery。不过,我会尝试将我的测试html页面放在IIS服务器上,看看这是否会改变什么。这是问题2。我把我的html文件放在承载我的服务的IIS应用程序上,它工作正常。如果IIS服务和网页位于不同的域上,是否有任何解决方法?是的,请使用JSONP而不是JSON。我的端点和端点行为(无合同名称、端点行为名称)是相同的。是否要我将配置添加到问题?请查看我的问题。我已经给了一个SSCE。
<bindings>
<webHttpBinding>
<binding name="WebBindingWithScripts"
crossDomainScriptAccessEnabled="true">
<security mode="None" />
</binding>
</webHttpBinding>
</bindings>
<endpoint name="RestWeb"
address="web"
binding="webHttpBinding"
behaviorConfiguration="WebEndpointBEhavior"
bindingConfiguration="WebBindingWithScripts"
contract="TestService.IRestService">
</endpoint>
<behaviors>
<serviceBehaviors>
......
</behavior>
</serviceBehaviors>
<endpointBehaviors>
<behavior name="EndpBehavior">
<webHttp/>
</behavior>
</endpointBehaviors>
<endpoint address="" binding="webHttpBinding" contract="IService1"
behaviorConfiguration="EndpBehavior">
[WebGet(ResponseFormat =WebMessageFormat.Json)]