doudunyi3796 2019-08-08 06:08
浏览 505

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>
  • 写回答

0条回答

    报告相同问题?

    悬赏问题

    • ¥15 素材场景中光线烘焙后灯光失效
    • ¥15 请教一下各位,为什么我这个没有实现模拟点击
    • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
    • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
    • ¥20 有关区间dp的问题求解
    • ¥15 多电路系统共用电源的串扰问题
    • ¥15 slam rangenet++配置
    • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
    • ¥15 ubuntu子系统密码忘记
    • ¥15 保护模式-系统加载-段寄存器