从jquery .ajax()中消费时,启用ajax的WCF服务(JSON)出错
本文关键字:ajax WCF 服务 JSON 启用 出错 jquery | 更新日期: 2023-09-27 18:14:44
不幸的是,它只在调用。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);
}
});
});
这个回复有点晚了,但是你把你的web方法定义为[WebGet]
,但是在你的Jquery Ajax方法中把它称为POST
请求。将[WebGet]
替换为如下内容:
[WebInvoke(Method = "POST", RequestFormat = WebMessageFormat.Json)]
.