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 关于#windows#的问题:怎么用WIN 11系统的电脑 克隆WIN NT3.51-4.0系统的硬盘
    • ¥15 matlab有关常微分方程的问题求解决
    • ¥15 perl MISA分析p3_in脚本出错
    • ¥15 k8s部署jupyterlab,jupyterlab保存不了文件
    • ¥15 ubuntu虚拟机打包apk错误
    • ¥199 rust编程架构设计的方案 有偿
    • ¥15 回答4f系统的像差计算
    • ¥15 java如何提取出pdf里的文字?
    • ¥100 求三轴之间相互配合画圆以及直线的算法
    • ¥100 c语言,请帮蒟蒻写一个题的范例作参考