Jquery对WCF的Ajax调用在Firefox中不起作用
嗨,我做了一个WCF服务,可以从Jquery访问它在IE8中工作,但在firefox中不工作 这是我的服务课Jquery对WCF的Ajax调用在Firefox中不起作用,jquery,wcf,internet-explorer,firefox,get,Jquery,Wcf,Internet Explorer,Firefox,Get,嗨,我做了一个WCF服务,可以从Jquery访问它在IE8中工作,但在firefox中不工作 这是我的服务课 namespace JSONSample { [ServiceContract] public interface IService1 { [OperationContract] [WebGet( ResponseFormat = WebMessageFormat.Json)] Employee[] getData(
namespace JSONSample
{
[ServiceContract]
public interface IService1
{
[OperationContract]
[WebGet( ResponseFormat = WebMessageFormat.Json)]
Employee[] getData();
}
[DataContract]
public class Employee
{
[DataMember]
public string id { get; set; }
[DataMember]
public string name { get; set; }
}
}
以及实施情况
namespace JSONSample
{
public class Service1 : IService1
{
#region IService1 Members
public Employee[] getData()
{
return new Employee[] { new Employee() { id = "1", name = "John" }, new Employee() { id = "2", name = "Bourne" }, new Employee() { id = "3", name = "Harry" } };
}
#endregion
}
}
Webconfig中的端点
我已经创建了端点
<system.serviceModel>
<behaviors>
<serviceBehaviors>
<behavior name="JSONSample.Service1Behavior">
<serviceMetadata httpGetEnabled="true"/>
<serviceDebug includeExceptionDetailInFaults="false"/>
</behavior>
</serviceBehaviors>
<endpointBehaviors>
<behavior name="web">
<webHttp/>
</behavior>
</endpointBehaviors>
</behaviors>
<services>
<service behaviorConfiguration="JSONSample.Service1Behavior" name="JSONSample.Service1">
<endpoint address="" binding="webHttpBinding" contract="JSONSample.IService1" behaviorConfiguration="web"/>
</service>
</services>
</system.serviceModel>
为什么这只适用于IE而不适用于firefox?您还必须在WCF服务中设置CORS头。您可以通过自定义行为轻松设置
检查这个。
你应该考虑使用ASP.NET的MVC3操作和JSORMEST…或MVC4WebAPI。jQuery.support.cors = true;
$(document).ready(function() {
$.ajax({
type: "GET",
url: "http://localhost:51220/Service1.svc/getdata",
contentType: "application/json; charset=utf-8",
dataType: "json",
processdata: false,
success: function(msg) {
alert(msg);
},
error: function ServiceFailed(result) {
alert('Service call failed: ' + result.status + '' + result.statusText);
}
});
});