JavaScript上的ajax中请求的资源上没有“Access-Control-Allow-Origin”标头[duplicate]

I am currently working on a spring project, and I am designating uri using a page controller. We need to use uri short API now. There is a problem. The way that we're doing it is PHP. But I have to use the code in JavaScript.

I tried this code.

    data = {};
    data.key = "ehelkhej45jjb38h6f5234234hg";
    data.short = "www.google.com";
    data.name = "googleuri"

    $.ajax({
        url : "https://cutt.ly/api/api.php",
        type : "POST",
        dataType : "json",
        beforeSend : function(xhr){
            xhr.setRequestHeader("Access-Control-Allow-Origin", "*");
            xhr.setRequestHeader("Content-type","application/x-www-form-urlencoded");
        },
        data : data,
        success : function(result) {
            console.log(result);
        }
    });

This has caused me an error.

Access to XMLHttpRequest at 'https://cutt.ly/api/api.php' from origin 'http://localhost:11000' 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. jquery-1.9.1.js:8526 POST https://cutt.ly/api/api.php net::ERR_FAILED

I could tried Postman

postman

When I first called this, the body was empty and status : 500Internal Server Error. But when I call again, I get a response like a picture.

And I could tried GET and jsonp but it was Blocked

    $.ajax({
        url : "https://cutt.ly/api/api.php",
        type : "GET",
        dataType : "jsonp",
        data : data,
        success : function(result) {
            console.log(result);
        }
    });

Block ERROR

jquery-1.9.1.js:8336 Cross-Origin Read Blocking (CORB) blocked cross-origin response https://cutt.ly/api/api.php?callback=jQuery191049576531804679713_1565245716711&key=ehelkhej45jjb38h6f5234234hg&short=www.google.com&name=googleuri&_=1565245716712 with MIME type text/html. See https://www.chromestatus.com/feature/5629709824032768 for more details.

What I've tried. It was worked. But it is error

GET http://cors-anywhere.herokuapp.com/https://cutt.ly/api/api.php?key=ehelkhej45jjb38h6f5234234hg&short=redisgate.kr&name=redisgate 500 (Internal Server Error)

var options = "https://cutt.ly/api/api.php?key=ehelkhej45jjb38h6f5234234hg&short=www.google.com&name=googleuri"


$.ajaxPrefilter( function (options) {
        if (options.crossDomain && jQuery.support.cors) {
          var http = (window.location.protocol === 'http:' ? 'http:' : 'https:');
          options.url = http + '//cors-anywhere.herokuapp.com/' + options.url;
          }
        });

$.get(
    options,
         function (response) {
             console.log("> ", response);
});

This link is the link that has API usage. How can you solve this problem?

</div>
dongxia1390
dongxia1390 看看我的问题。等待响应值时出错。键值不正确,您无法以这种方式查看结果。
大约一年之前 回复
doujia4041
doujia4041 当我运行您更新的示例时,我得到{“url”:{“status”:4}}。似乎代理运行有时候非常慢(对我来说花了8秒钟)。如果这是您在代理完成后看到的内容,则api密钥无效。
大约一年之前 回复
dounao4179
dounao4179 我把这个问题弄清楚了。有一个我已经纠正错误的代码,但它没有给我任何响应值。是什么原因?
大约一年之前 回复
dua6992
dua6992 我通过引用您链接的答案解决了错误,但没有返回值。为什么?
大约一年之前 回复
dongyongju9560
dongyongju9560 您在POST上收到CORS错误可能是因为该API不支持POST。当您在jQuery中尝试GET时,您会收到CORB错误,因为当应该响应(可能)类型为application/json时,该API会使用内容类型text/html响应您的GET。您的浏览器将阻止来自跨源请求的text/html作为安全措施。看起来API实际上只是用于服务器端。
大约一年之前 回复
Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!
立即提问