jQuery JSONP 使用的 CrossDomain WCF 返回 undefined

Posted

技术标签:

【中文标题】jQuery JSONP 使用的 CrossDomain WCF 返回 undefined【英文标题】:CrossDomain WCF consumed by jQuery JSONP returns undefined 【发布时间】: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 使用它。谢谢。 【参考方案1】:

引用自following article:

在 Cassini 中运行时一切正常,但是当您想要 在 IIS 中托管它,您需要注意两件事。首先,你会 需要确保正确安装 IIS 和 WCF 并且 挂号的。如果 IIS 似乎不想把你的 SVC 文件拿出来 (您可以通过尝试 http:////AjaxService.svc 来检查),尝试运行命令 ServiceModelReg.exe /i /x 来自 %WINDIR%\Microsoft.NET\Framework\v3.0\Windows 通信 基础。运行此命令后重新启动 IIS。其次,如果你 创建了一个默认的 ASP.NET 2.0 网站,你可能会遇到这个错误 消息:

IIS specified authentication schemes 'IntegratedWindowsAuthentication, Anonymous', but the binding only supports specification of exactly one authentication scheme. Valid authentication schemes are Digest, Negotiate, NTLM, Basic, or Anonymous. Change the IIS settings so that only a single authentication scheme is used.

要解决此问题,请右键单击 您的 IIS 管理器并选择“属性”。然后点击“目录” 选项卡,单击“匿名访问和 身份验证控制”区域。您将看到这两个匿名访问 并选择集成 Windows 身份验证。取消选择其中之一 他们,然后重新启动 IIS。之后,应用程序应该可以工作 顺利。

在您的情况下,将集成 Windows 身份验证替换为基本身份验证。

【讨论】:

我刚刚在本地 IIS 上对其进行了测试,并且运行良好。我不确定我对我的 Godaddy 主机上的 IIS 有那么多的控制权。我只在他们的 IIS 管理器 x-匿名访问、目录浏览、x-Set Application Root 和 anon 中看到这 3 个选项,并且设置 root 已打开。我已经打开了一张带有此帖子链接的票。谢谢。 刚刚尝试在 Godaddy IIS 管理器中为应用程序文件夹关闭匿名开关。同样的问题。我看到一个带有经典和集成选项的管道设置。不确定安全性是否会起作用,因为我没有他们的帐户。重新设置匿名。 对不起,一场一场比赛……就这么近。只是将我的路由器指向我客户的网络端口并尝试使用我的主机名进行访问,但由于未定义而失败。将其设置回localhost:8081 并且它可以工作。可能是该测试的环回问题,但似乎与安全相关。 jsfiddle.net/fqZtF 本地通过主机或 losthost url,解决方案现在总是有效jsfiddle.net/fqZtF。我只是遇到了防火墙问题。 IIS 设置为仅匿名和 .net 4.0。在 GoDaddy,该解决方案仅根据此列表 jsfiddle 链接生成 UNDEFINED:jsfiddle.net/TyrHW

以上是关于jQuery JSONP 使用的 CrossDomain WCF 返回 undefined的主要内容,如果未能解决你的问题,请参考以下文章

使用 JSONP 时如何捕获 jQuery $.getJSON(或数据类型设置为“jsonp”的 $.ajax)错误?

jQuery使用JSONP实现跨域请求

jQuery + JSONP + 雅虎查询语言

Jquery成功函数未使用JSONP触发

html jQuery,JSON:使用JSONP的基本jQuery示例

如何使用 jquery 读取 Jsonp [关闭]