1

我正在关注这个链接:

http://aspdotnetcodebook.blogspot.in/2012/02/calling-cross-domain-wcf-service-using.html

为了实现跨域,它在 consol 应用程序中工作,但在 svc web wcf 服务应用程序中不起作用。

我应该怎么办 ?

编辑:

[ServiceContract]
    public interface IOrderService
    {
        [OperationContract]
        [WebInvoke(Method = "GET", UriTemplate = "/", ResponseFormat = WebMessageFormat.Json)]
        string ProcessOrder();                                                              
    }        
namespace WFS.Services
    {
        [ServiceBehavior]
        public class OrderService : IOrderService
        {
            [OperationBehavior]
            public string ProcessOrder()
            {
                Order order = new Order
                {
                    ID = 1,
                    OrderDate = DateTime.Now,
                    Name = "Laptop"

                };

                DataContractJsonSerializer json = new DataContractJsonSerializer(typeof(Order));
                MemoryStream stream = new MemoryStream();
                json.WriteObject(stream, order);
                return Encoding.UTF8.GetString(stream.ToArray());
            }
        }
    }

和配置代码:

configuration>
<system.serviceModel>
    <services>
        <service name="WFS.Services.OrderService" behaviorConfiguration="serviceBehave">
            <endpoint address=""
                      bindingConfiguration="crossDomain"
                      behaviorConfiguration="enableScriptBehaviour"
                      binding="webHttpBinding"
                      contract="WFS.Services.IOrderService"/>
            <host>
                <baseAddresses>
                    <add baseAddress="http://localhost:4916/"/>
                </baseAddresses>
            </host>
        </service>
    </services>
    <behaviors>
        <serviceBehaviors>
            <behavior name="serviceBehave">
                <serviceMetadata httpGetEnabled="true" httpGetUrl="mex"/>
                <serviceDebug includeExceptionDetailInFaults="true" />
            </behavior>
        </serviceBehaviors>
        <endpointBehaviors>
            <behavior name="enableScriptBehaviour">
                <webHttp/>
            </behavior>
        </endpointBehaviors>
    </behaviors>
    <bindings>
        <webHttpBinding>
            <binding name="crossDomain" crossDomainScriptAccessEnabled="true"/>
        </webHttpBinding>
    </bindings>
</system.serviceModel>
<system.web>
    <compilation debug="true"/>
</system.web>

这是 json 获取:

$.getJSON("http://localhost:4916/Order.svc?ProcessOrder?callback?", null, function (data) {
                alert(data);
            });

它给了我解析器错误 200 成功

这是错误

4

1 回答 1

1

我找到了 :

只需在 svc 服务的标记上添加 'Factory="System.ServiceModel.Activation.WebScriptServiceHostFactory"' 代码。

<%@ ServiceHost Language="C#" Debug="true" Service="WFS.Services.Order" CodeBehind="Order.svc.cs"
Factory="System.ServiceModel.Activation.WebScriptServiceHostFactory" %>

然后我更正了我的配置文件:

    <configuration>
  <system.web>
    <compilation debug="true" targetFramework="4.0" />
    <authentication mode="None" />
  </system.web>
  <system.webServer>
    <modules runAllManagedModulesForAllRequests="true"/>
  </system.webServer>
  <system.serviceModel>
    <serviceHostingEnvironment aspNetCompatibilityEnabled="true"/>
    <standardEndpoints>
      <webScriptEndpoint>
        <standardEndpoint name="" crossDomainScriptAccessEnabled="true"/>
      </webScriptEndpoint>
    </standardEndpoints>
  </system.serviceModel>
</configuration>

添加属性到我的服务类:

[ServiceContract(Namespace = "JsonpAjaxService")]
    [AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Allowed)]

在 jquery 方面我已经更正了 proccessData:false

        <script>
        $(document).ready(function () {
            $.ajax({
                url: 'http://localhost:4916/Order.svc/GetCustomer',
                type: 'GET',
                dataType: 'jsonp',
                data: {},
                processdata: false,
                success: function (result) {
                    //it comes here successfuly
                    debugger;
                },
                error: function (err, xhr, res) {
                    debugger;
                }
            });
        });
    </script>
于 2013-08-13T08:30:11.543 回答