【问题标题】:Using DocRaptor web service with AngularJS?将 DocRaptor Web 服务与 AngularJS 一起使用?
【发布时间】:2015-03-23 20:32:45
【问题描述】:

我正在尝试使用 DocRaptor 从 AngularJS 应用程序生成 Excel (.xls) 文件。在尝试 DocRaptor 入门文档中描述的简单 POST 请求时,我不断收到以下错误。他们的文档声称他们支持 CORS 请求。任何帮助将不胜感激。

Chrome 中的错误消息:

OPTIONS https://docraptor.com/docs?user_credentials=my-api-key

XMLHttpRequest cannot load https://docraptor.com/docs user_credentials=my-api-key. 

No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http://localhost:8000' is therefore not allowed access. The response had HTTP status code 404.

这是我的 AngularJS 控制器中的 JavaScript 代码:

var forDocRaptor = {
    "doc" : {
        "test": true,
        "document_type": "pdf",
        //Just trying simple pdf for now
        "name": "adoc.pdf",
        "document_content": '<div>PDF generation test</div>',
        "strict": "none",
        "javascript": true,
        "tag": "tag",
        "async": false
    }
}   

$scope.exportToExcel = function() {

    $http.post('https://docraptor.com/docs?user_credentials=my-api-key', forDocRaptor)
        .success(function(res) {
            console.log(res)
    });

}

【问题讨论】:

    标签: javascript angularjs


    【解决方案1】:

    我遇到了同样的问题。你应该发送 -

    data:forDocRaptor
    

    另外,如果你使用

    method:'POST'
    

    有效,但 $http.post 无效

    所以 - 你的代码应该是这样的:

    $http({
         method:'POST',
         url: URL,
         data:forDocRaptor
      }).success(function(res){
         console.log(res)
    })
    

    【讨论】:

      猜你喜欢
      • 2015-02-02
      • 1970-01-01
      • 1970-01-01
      • 2011-08-20
      • 2011-01-08
      • 2015-09-30
      • 2023-03-23
      • 1970-01-01
      • 2012-05-22
      相关资源
      最近更新 更多