英文:
Deepl Api request blocked by CORS policy
问题
我已经在我们的Web CRM中集成了Deepl API几个月了。直到几天前都正常运行。我不知道确切的时间,但可能是从新年开始。
现在,所有请求都被CORS策略阻止:
来自来源 'https://example.com' 对 'https://api-free.deepl.com/v2/translate?auth_key=xxxxxxxxxxxxxxxx:fx&target_lang=DE&preserve_formatting=1&split_sentences=0&text=bonjour' 的XMLHttpRequest访问已被CORS策略阻止:响应预检请求未通过访问控制检查:请求的资源上没有 'Access-Control-Allow-Origin' 头。
我通过jQuery ajax发送请求:
var url = "https://api-free.deepl.com/v2/translate?auth_key=xxxxxxxxxxxxxxxx:fx&target_lang=DE&preserve_formatting=1&split_sentences=0&text=bonjour";
$.ajax({
url,
type: 'GET',
success: (translations) => {
console.log(translations);
}
});
有什么想法吗?
我尝试从http
的本地网站和https
的生产网站中都尝试过,结果都一样。
在Postman应用中可以正常工作。
英文:
I have integrated Deepl API in our Web CRM since a few months. It was working fine until a few days ago. I don't know when exactly, but maybe since the new year.
Now, all requests are blocked by CORS policy :
> Access to XMLHttpRequest at 'https://api-free.deepl.com/v2/translate?auth_key=xxxxxxxxxxxxxxxx:fx&target_lang=DE&preserve_formatting=1&split_sentences=0&text=bonjour' from origin 'https://example.com' has been blocked by CORS policy: Response to preflight request doesn't pass access control check: No 'Access-Control-Allow-Origin' header is present on the requested resource.
I'm sending requests via jQuery ajax :
var url = "https://api-free.deepl.com/v2/translate?auth_key=xxxxxxxxxxxxxxxx:fx&target_lang=DE&preserve_formatting=1&split_sentences=0&text=bonjour";
$.ajax({
url,
type: 'GET',
success: (translations) => {
console.log(translations);
}
});
Any ideas why please ?
I tried from local website in http
and from production site in https
, same result.
It's working well with Postman app.
答案1
得分: 2
我通过在我的服务器上安装Deepl库(在我的情况下是Php)来解决了我的问题。
而不是直接从我的JS调用Deepl API,我将我的请求发送到了我的服务器。
有关客户端库的更多信息,请查看:https://www.deepl.com/fr/docs-api/api-access/client-libraries/
感谢@John Smith
英文:
I solved my issue by installing the Deepl library on my server (Php in my case).
Instead of calling the Deepl API directly from my JS, I send my requests to my server.
More info on Client libraries : https://www.deepl.com/fr/docs-api/api-access/client-libraries/
Thanks to @John Smith
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论