使用JavaScript POST请求OCR.space API遇Error 99:文件类型识别失败
解决OCR.space API文件类型识别错误的方案
错误提示明确要求手动指定文件类型,只需在请求参数中添加filetype字段即可解决问题:
修改后的OCR函数
async function OCR(jpg) { const url = 'https://api.ocr.space/parse/image'; const data = { apikey: 'SECRET', base64Image: jpg, filetype: 'jpg' // 新增:明确指定文件类型为JPG }; const options = { method: 'POST', headers: { 'Content-Type': 'application/json', }, body: JSON.stringify(data), }; try { const response = await fetch(url, options); const json = await response.json(); return json; } catch (error) { console.error(error); return { error: true }; } }
补充说明
你的toBase64函数返回的带data:image/jpeg;base64,前缀的Base64字符串是符合API要求的,无需额外处理前缀。添加filetype参数后,API就能准确识别文件格式,消除E216错误。
内容的提问来源于stack exchange,提问作者Marley Lamparter
相关产品推荐
相关产品推荐

