我正在关注此链接:
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 get:
$.getJSON("http://localhost:4916/Order.svc?ProcessOrder?callback?", null, function (data) {
alert(data);
});
它给了我解析器错误200成功
答案 0 :(得分: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>