使用带有AngularJS的DocRaptor web服务

Using DocRaptor web service with AngularJS?

本文关键字:DocRaptor web 服务 AngularJS      更新时间:2023-09-26

我正在尝试使用DocRaptor从AngularJS应用程序生成Excel(.xls)文件。在尝试DocRaptor的getting Started文档中描述的简单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)
    });
}

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

data:forDocRaptor

此外,出于某种原因,如果您使用

method:'POST'

它有效,但$http.post不能

所以,你的代码看起来是这样的:

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