1

嗨,我做了一个 WCF 服务,可以从 Jquery 访问它在 IE 8,IE 中工作,但在 Firefox 中不工作。

这是我的服务班

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>

来自 Html 的调用我正在使用 get 并且没有向服务器发送任何数据

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


        });

为什么这只适用于 IE 而不是 Firefox

4

1 回答 1

0

您还必须在 WCF 服务中设置 CORS 标头。您可以通过自定义行为轻松设置它。

检查这篇文章

于 2012-07-05T05:37:34.493 回答