我期待以下代码执行Ajax调用。但是,我看到405错误,说
请求的资源不支持http方法' GET'。
如果我使用类型=' json' 我会
请求的资源不支持http方法' JSON'。
以下是我的内容脚本代码的相关部分
$(document).ready(function() {
var imgSrcArr = [];
$('img').each(function(e) {
var s = this.src;
imgSrcArr.push(s);
$.ajaxSetup({cache: false});
var formData = new FormData();
formData.append("apikey", "xxxxxxxxxxxxxxx");
formData.append("isOverlayRequired", false);
formData.append("url", s);
//console.log(formData);
//console.log($.support.cors);
$.ajax({
url: "https://api.ocr.space/parse/image",
data: {apikey: "xxxxxxxxxxxxxxx", url: s},
//method: 'GET',
dataType: 'json',
async: false,
cache: false,
contentType: "application/json; charset=utf-8",
processData: false,
type: 'GET',
crossDomain: true,
xhrFields: {
// The 'xhrFields' property sets additional fields on the XMLHttpRequest.
// This can be used to set the 'withCredentials' property.
// Set the value to 'true' if you'd like to pass cookies to the server.
// If this is enabled, your server must respond with the header
// 'Access-Control-Allow-Credentials: true'.
withCredentials: true
},
headers: {
// Set any custom headers here.
// If you set any non-simple headers, your server must include these
// headers in the 'Access-Control-Allow-Headers' response header.
},
success: function(ocrParsedResult, textStatus, jqXHR) {
//Get the parsed results, exit code and error message and details
var parsedResults = ocrParsedResult["ParsedResults"];
var ocrExitCode = ocrParsedResult["OCRExitCode"];
var isErroredOnProcessing = ocrParsedResult["IsErroredOnProcessing"];
var errorMessage = ocrParsedResult["ErrorMessage"];
var errorDetails = ocrParsedResult["ErrorDetails"];
var processingTimeInMilliseconds = ocrParsedResult["ProcessingTimeInMilliseconds"];
//If we have got parsed results, then loop over the results to do something
if (parsedResults!= null) {
//Loop through the parsed results
$.each(parsedResults, function (index, value) {
var exitCode = value["FileParseExitCode"];
var parsedText = value["ParsedText"];
var errorMessage = value["ParsedTextFileName"];
var errorDetails = value["ErrorDetails"];
var textOverlay = value["TextOverlay"];
var pageText = '';
switch (+exitCode) {
case 1:
pageText = parsedText;
break;
case 0:
case -10:
case -20:
case -30:
case -99:
default:
pageText += "Error: " + errorMessage;
break;
}
console.log(parsedText);
var bazExtract = extractEmails1(parsedText);
if(bazExtract !== null) {
extractEmails2(bazExtract);
}
});
}
},
error: function(err) {
console.log(err);
}
});
});
//console.log(imgSrcArr);
});
注意:内容脚本代码的其他部分正在按预期运行,但是没有服务器响应(在开发人员控制台中)到Ajax请求。
如果这是一个CORS问题,是否有针对此类错误的解决方法?我在SO上尝试了一些关于类似问题的答案,但是我无法继续推进其中任何一个问题。