【发布时间】:2013-01-11 01:28:40
【问题描述】:
此示例归功于 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." };
}
}
}
web.config
<?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 获得未定义。
这对我来说是全新的......所以这是一项使命。 谢谢。
【问题讨论】:
-
我会避免使用 JSONP,因为它会带来安全风险恕我直言...
-
我会对客户端代码替代方案感兴趣。它需要是 WCF,因为我需要从 SharePoint 365 Online 使用它。谢谢。