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 关于docker部署flink集成hadoop的yarn,请教个问题 flink启动yarn-session.sh连不上hadoop,这个整了好几天一直不行,求帮忙看一下怎么解决
    • ¥30 求一段fortran代码用IVF编译运行的结果
    • ¥15 深度学习根据CNN网络模型,搭建BP模型并训练MNIST数据集
    • ¥15 C++ 头文件/宏冲突问题解决
    • ¥15 用comsol模拟大气湍流通过底部加热(温度不同)的腔体
    • ¥50 安卓adb backup备份子用户应用数据失败
    • ¥20 有人能用聚类分析帮我分析一下文本内容嘛
    • ¥30 python代码,帮调试,帮帮忙吧