问题描述:

Hi I made a WCF service to be accessed from Jquery it works in IE 8,IE but its not working in firefox.

This is my service class

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; }

}

}

And the implementation

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

}

}

Endpoints in Webconfig

I have created the end points

<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>

The call from Html i'm using a get and not sending any data to the server

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);

}

});

});

why is this only working in IE and not firefox

网友答案:

You have to set the CORS headers in WCF service too. You can easily set that through a custom behavior.

Check this post.

相关阅读:
Top