weixin_33724059 2016-03-12 15:40 采纳率: 0%
浏览 805

http服务器上的CORS失败

I am using a simple http-server while developing a javascript app in which there are with several ajax requests to external APIs. I started the server with "http-server --cors" via CLI, but still get "No 'Access-Control-Allow-Origin' header is present on the requested resource" errors. This occurs whether or not the external API requires a user key/pre-flight, such as:

var url = "http://api.rgrta.com/";
$.ajax({
    url: url,
    beforeSend: function(xhrObj){
        xhrObj.setRequestHeader("api_key","d4f...3f5");
    },
    type: "GET",
    dataType: "json"
})
.done(function(jsonData) {
    console.log("*** ajax success ***");
    callbackFunc();

    function callbackFunc() {
        console.log("callbackFunc");
    }
});

I've seen solutions involving express app.js configurations. Is node/express the only way to define request headers properly? What is the best way to set up my local http-server when developing for CORS-enabled API calls?

  • 写回答

2条回答 默认 最新

  • 撒拉嘿哟木头 2016-03-12 16:00
    关注

    That's not a server side code snippet which you have provided, rather its client side code which runs over browser, and hence cross origin errors would occur if the target api has cors turned off, the solution in that case would be to use a proxy.

    Also enabling cors on simple http server means that you are allowing your website content to be accessed elsewhere by others.

    评论

报告相同问题?

悬赏问题

  • ¥20 BAPI_PR_CHANGE how to add account assignment information for service line
  • ¥500 火焰左右视图、视差(基于双目相机)
  • ¥100 set_link_state
  • ¥15 虚幻5 UE美术毛发渲染
  • ¥15 CVRP 图论 物流运输优化
  • ¥15 Tableau online 嵌入ppt失败
  • ¥100 支付宝网页转账系统不识别账号
  • ¥15 基于单片机的靶位控制系统
  • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?