Deepl API 请求被跨源资源共享策略拦截。

huangapple go评论70阅读模式
英文:

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

huangapple
  • 本文由 发表于 2023年1月9日 17:04:26
  • 转载请务必保留本文链接:https://go.coder-hub.com/75055049.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定