此示例归功于 Pranay Rana:http: //www.codeproject.com/Articles/223572/Calling-Cross-Domain-WCF-service-using-Jquery-Java
在此处失败并链接到实际 WCF 的问题:http: //jsfiddle.net/TyrHW/
标记:
<%@ ServiceHost Language="C#" Debug="true" Service="WCF1.Service1" CodeBehind="Service1.svc.cs" Factory="System.ServiceModel.Activation.WebScriptServiceHostFactory"%>
C# WCF 服务
using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.ServiceModel.Activation;
using System.ServiceModel.Web;
using System.Text;
namespace WCF1
{
[DataContract]
public class Customer
{
[DataMember]
public string Name;
[DataMember]
public string Address;
}
[ServiceContract(Namespace = "JsonpAjaxService")]
[AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Allowed)]
public class Service1
{
[WebGet(ResponseFormat = WebMessageFormat.Json)]
public Customer GetCustomer()
{
return new Customer() { Name = "Jacob Pines", Address = "999 S William St." };
}
}
}
网络配置
<?xml version="1.0"?>
<configuration>
<system.web>
<compilation debug="true" targetFramework="4.0" />
</system.web>
<system.serviceModel >
<serviceHostingEnvironment aspNetCompatibilityEnabled="true" multipleSiteBindingsEnabled="true"/>
<standardEndpoints>
<webScriptEndpoint>
<standardEndpoint name="" crossDomainScriptAccessEnabled="true"/>
</webScriptEndpoint>
</standardEndpoints>
</system.serviceModel>
<system.webServer>
<modules runAllManagedModulesForAllRequests="true"/>
</system.webServer>
<system.web>
<customErrors mode="Off"/>
</system.web>
</configuration>
我在本地得到与我的 godaddy .net 4.0 主机相同的结果。我检查了 asp.net 级别。goddady 的 IIS 安全设置为匿名。如果我在浏览器中输入服务的 url,我会收到一些明显的身份验证问题,我已经发送给 Godaddy。如果我在本地做同样的事情,我会得到看起来很健康的 Web 服务,尽管仍然从 jsFiddle 中得到未定义。
这对我来说都是新的......所以这是一项使命。谢谢。