在AngularJS中使用DocRaptor Web服务?

时间:2015-03-23 20:32:46

标签: javascript angularjs

我正在尝试使用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)
    });

}

1 个答案:

答案 0 :(得分:3)

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

data:forDocRaptor

另外,出于某种原因,如果你使用

method:'POST'

它有效,但$ http.post不

所以 - 你的代码看起来像这样:

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