WinJS - XHR - 设置引用者是不是不可能?

时间:2013-01-18 13:57:51

标签: javascript windows-8 xmlhttprequest winjs

我正在为一家使用Referer的公司开发WinJS应用程序。

不幸的是我找不到办法做到这一点,甚至连曲奇都没有!


这是一个代码示例:

Q.when(WinJS.xhr({
  url: "http://localhost:8888/api/auth/",
  type: "GET",
  headers: {
    "If-Modified-Since": "Mon, 27 Mar 1972 00:00:00 GMT",
    "Referer": "http://localhost:8888/api/"
  }
}));

WinRT似乎使用IE和其他浏览器所做的相同行为。从请求中删除Referer和Cookie标头的位置。

有任何解决方法吗?

2 个答案:

答案 0 :(得分:0)

如果您使用Referer进行身份验证,则应考虑切换到正确的身份验证系统。类似API键的东西。

Referer从未被设计为一种身份验证工具,所以它在这方面做得非常糟糕。

答案 1 :(得分:0)

使用AtomPubClient作为解决方法怎么样?

试试这个:

function doRequest3() {
    var reader;
    var client = Windows.Web.AtomPub.AtomPubClient();
    var uri = new Windows.Foundation.Uri("http://example.com");
    client.setRequestHeader("Referer", "http://localhost:8888/api/");
    client.retrieveMediaResourceAsync(uri).then(function(stream)
    {
        reader = Windows.Storage.Streams.DataReader(stream);
        return reader.loadAsync(999999);
    }).done(function (bytesRead) {
        var contentString = reader.readString(bytesRead);
        document.getElementById("content").innerText += "Content: " + contentString;
    }, function (error) {
        console.log(error);
    });
}