我正在构建一个Ionic 2(ts)应用程序,该应用程序将向OCR.space API发送REST调用。通过他们的例子,我能够通过HTTP.post发送一个Base64Image,但是当试图通过HTTP.Post发送文件时,我遇到了:
{"ParsedResults":null,"OCRExitCode":0,"IsErroredOnProcessing":false,"ErrorMessage":["Parameter
name 'file' is invalid. Valid parameters:
apikey,url,language,isoverlayrequired,base64image"],"ErrorDetails":null,"ProcessingTimeInMilliseconds":"1"}
我猜这是我的帖子请求的格式:
HTTP.post('http://api.ocr.space/parse/image',
{ "apikey":"helloworld", "language":"eng", "isOverlayRequired":"false", "file": "asssets/img/test2.pdf" }, {})
.then(data => {
console.log("HTTP entered");
let result = JSON.parse(data.data); // data received by server
console.log(data.data);
})
.catch(error => {
console.log(error.error); // error message as string
});
我猜这是因为我能够通过邮递员成功发送PDF文件,如下所示:my successful postman request
所以 - 我希望能帮助您找到如何成功发送此HTTP.post请求或将我可以从postman获得的代码转换为成功的离子原生语法。
var form = new FormData();
form.append("apikey", "541496f13e88957");
form.append("language", "eng");
form.append("isOverlayRequired", "false");
form.append("file", "1page.pdf");
var settings = {
"async": true,
"crossDomain": true,
"url": "https://api.ocr.space/parse/image",
"method": "POST",
"headers": {
"cache-control": "no-cache",
"postman-token": "1aea47d5-a0eb-7768-5fa6-60c4cd76d453"
},
"processData": false,
"contentType": false,
"mimeType": "multipart/form-data",
"data": form
}
$.ajax(settings).done(function (response) {
console.log(response);
});
我感谢所有人的帮助!