不幸的是,只有在调用.ajax()时才会遇到错误情况,而textStatus(第二个参数)只会说“错误”。我已经阅读了关于stackoverflow的几个例子和其他问题,但必须遗漏一些东西。提前感谢您的帮助。
WCF服务:
[ServiceContract(Namespace = "http://localhost/")]
[AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Allowed)]
public class Items
{
[OperationContract]
[WebGet]
public string HelloWorld()
{
return "Hello World.";
}
}
WCF Web.config
仅限相关部件...... 服务:
<services>
<service name="OService.ReadServices.Items">
<endpoint address="soap" binding="basicHttpBinding" contract="OService.ReadServices.Items"/>
<endpoint address="json" binding="webHttpBinding" behaviorConfiguration="jsonBehavior" contract="OService.ReadServices.Items"/>
</service>
</services>
行为:
<endpointBehaviors>
<behavior name="jsonBehavior">
<enableWebScript />
</behavior>
</endpointBehaviors>
Jquery的
$(document).ready(function () {
$.ajax({
type: "POST",
contentType: "application/json; charset=utf-8",
url: "http://localhost/OService/ReadServices/Items.svc/json/HelloWorld",
data: "{}",
dataType: "json",
success: function (msg) {
alert("success: " + msg.d);
},
error: function (xhr, textStatus, errorThrown) {
alert("error: " + textStatus + " - " + errorThrown + " - " + xhr);
}
});
});
答案 0 :(得分:2)
此回复有点迟了,但您将Web方法定义为[WebGet]
,但在Jquery Ajax方法中将其称为POST
请求。将[WebGet]
替换为:
[WebInvoke(Method = "POST", RequestFormat = WebMessageFormat.Json)]
。