【问题标题】:Cross browser scripting proxy跨浏览器脚本代理
【发布时间】:2010-09-19 07:55:56
【问题描述】:

我正在开发一些在不同域上使用一些 JSON Web 服务的客户端 Javascript。我读过一些浏览器不允许跨域脚本,我应该在我的本地服务器上创建一个代理来提供数据。

谁能给我一个简单的例子来说明如何在 ASP.Net 中做到这一点?

【问题讨论】:

    标签: asp.net javascript web-services cross-domain


    【解决方案1】:

    没有浏览器允许跨域脚本,尽管 w3c 在其关于 xmlHTTPRequest-object 的建议中为此留出了空间,但我们仍然需要等待一段时间才能看到它以安全的方式实现方式...

    【讨论】:

    • IE8(测试版)有一个跨域请求的实现......但同样,你需要等待。
    【解决方案2】:

    我将为寻求问题一般答案的人们提供一个伪代码版本。

    SomeAjaxAbstraction.Request('proxyScript', {
        parameters: {
            address: 'http://somewhere.com/someapi?some=query'
        }
    });
    

    然后在proxyScript中:

    var address = GET['address'];
    if(ValidUrl(address) && ConnectionAllowed(address)) {
        // Validating address and whitelisting services is an exercise to the reader
        var response = SomeHttpGetFunction(address);
        echo XssAndBadStuffFilter(response);
    } else {
        // Handle errors
    }
    

    【讨论】:

      【解决方案3】:

      您可以使用JSONP 之类的技术来避免使用代理。假设您正在与之交谈的 Web 服务支持 JSONP(例如,Flickr 或 Twitter 都提供 JSONP API)或者您可以控制 Web 服务发回的数据,您可以使用具有 JSONP 功能的库在域之间发送 JSON 数据.

      例如,在 jQuery 中,您可以进行远程 JSON 调用:

      jQuery.getJSON("http://www.someothersite.com/webservice?callback=?", function(result)
      {
          doStuffWithResult(result);
      });
      

      由于调用的是另一个域,jQuery 会自动使用一些技巧来进行跨域调用。 jQuery 会自动替换 ?在带有回调函数名称的 url 中,Web 服务可以使用它来格式化返回的 JSON 数据。

      如果您是 Web 服务的控制者,您可以通过获取名为“callback”的请求参数来处理 JSONP 请求,该参数将设置为您需要使用的回调函数名称。回调函数接受一个参数,即您要发回的 JSON 数据。因此,如果回调参数设置为“jsonp2342342”,您将希望 Web 服务响应如下:

      jsonp2342342({key: value, key2: value});
      

      如果您使用的网络服务已经支持 JSONP,您就不必担心自己进行格式化。

      【讨论】:

        【解决方案4】:

        一般来说,代理在您的 Web 服务器上运行(在您的情况下很可能是 IIS),并将请求“中继”到不同域上的另一台服务器。

        这是一个在 C# .NET 中实现的示例

        Fast, Streaming AJAX proxy

        【讨论】:

        • +1 用于回答提出的问题与尝试强制提供不同的策略。 FWIW,您链接中的项目已移动,如果可能,请更新您的答案:codeproject.com/Articles/25218/…
        【解决方案5】:

        您可以编写一个简单的 .NET 页面来检索远程页面并将其显示在您的站点上:

        using System;
        using System.Collections.Generic;
        using System.Linq;
        using System.Web;
        using System.Web.UI;
        using System.Web.UI.WebControls;
        using System.Net;
        using System.IO;
        
        namespace Proxy
        {
            public partial class _Proxy : System.Web.UI.Page
            {
                protected void Page_Load(object sender, EventArgs e)
                {
                    string proxyURL = string.Empty;
                    try
                    {
                        proxyURL = HttpUtility.UrlDecode(Request.QueryString["u"].ToString());
                    }
                    catch { }
        
                    if (proxyURL != string.Empty)
                    {
                        HttpWebRequest request = (HttpWebRequest)WebRequest.Create(proxyURL);
                        request.Method = "GET";
                        HttpWebResponse response = (HttpWebResponse)request.GetResponse();
        
                        if (response.StatusCode.ToString().ToLower() == "ok")
                        {
                            string contentType = response.ContentType;
                            Stream content = response.GetResponseStream();
                            StreamReader contentReader = new StreamReader(content);
                            Response.ContentType = contentType;
                            Response.Write(contentReader.ReadToEnd());
                        }
                    }
                }
            }
        }
        

        请参阅我的帖子:http://www.johnchapman.name/aspnet-proxy-page-cross-domain-requests-from-ajax-and-javascript/

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2014-09-15
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2012-05-25
          相关资源
          最近更新 更多