【问题标题】:office excel making a CORS Request as cross domain requestoffice excel将CORS请求作为跨域请求
【发布时间】:2016-08-02 23:07:47
【问题描述】:

我正在尝试从我的 excel 插件发出跨域请求, 正如这里所建议的那样:http://dev.office.com/docs/add-ins/develop/addressing-same-origin-policy-limitations

我正在尝试实现一个 cors 请求。 但是,每当我尝试打开我的请求时,我都会收到拒绝访问错误。

我在这里使用的代码尝试打开连接,但是在这里我已经收到错误

 var xhr = new XMLHttpRequest();
xhr.open('GET', 'url'); //this line gives an acces denied error.
xhr.onload = function(e) {   

    }
xhr.send();

谁能告诉我是否可以在 excel 中实现 cors 请求?

带有 ajax 的代码也给出了一个错误对象

    $.ajax({
    type: 'GET',
    url: 'remoteurl',
    cache: true, 

    },
    success: function (result) {
        functionAfterLoad(JSON.parse(JSON.stringify(eval("(" +result + ")"))));

    },
    error: function(result) {
        console.error('Error while retrieving funnel HTML', result);
    },

});

但是当我打电话给:

    $.ajax({
    type: 'GET',
    url: window.location.href ,
    cache: true, 

    },
    success: function (result) {
        functionAfterLoad(JSON.parse(JSON.stringify(eval("(" +result + ")"))));

    },
    error: function(result) {
        console.error('Error while retrieving funnel HTML', result);
    },

});

我确实得到了 html 页面,结果没有任何错误

【问题讨论】:

    标签: javascript excel office-js


    【解决方案1】:

    HTML5 Rocks 有一些关于在客户端发出 CORS 请求的好技巧。在服务器上完成了很多 CORS 设置,但假设服务器设置为允许您的请求,您可能只需要在您的 open 调用之前包含 xhr.withCredentials = true;

    【讨论】:

    • 嘿@leetibbett 我得到一个运行时错误,访问被拒绝,即使我添加了 xhr.withCredentials = true;标志。
    • 您的 IE 设置是否允许 CORS?去哪里看this post的答案
    • 是的,我的ie设置允许CORS,但是我不知道excel webview是否使用了所有的ie设置?
    • 这是您的响应错误的样子吗:在 Access-Control-Allow-Origin 标头中找不到 Origin ms-appx-web://。 XMLHttpRequest:网络错误 0x80070005,访问被拒绝。
    • 我在这里的示例中添加了一个 ajax get 我得到一个包含错误的错误对象:访问被拒绝作为错误文本
    猜你喜欢
    • 2016-05-05
    • 2014-12-21
    • 2020-12-08
    • 2020-08-16
    • 2012-11-06
    • 2016-09-12
    • 2012-12-20
    • 2019-10-21
    • 2016-02-24
    相关资源
    最近更新 更多