weixin_33693070 2015-05-14 05:26 采纳率: 0%
浏览 41

将ajax调用翻译为CORS

I am trying to access an API via my localhost using the following code:

  $.ajax({
            url: "http://domain.xxx.net/api/tokens.json?email=xxx@xxx.net&password=xxx",
            //method: "GET",
            headers: { "Accept": "application/xml", "Content-Type": "application/xml", "Access-Control-Allow-Origin": "*"},
            dataType: "JSON",
            success:function(data){
                alert(data);},
            success:function(e){
                alert(e.message);}

    });

I have read over the internet that this is not possible due to the error below.

No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http://localhost:3000' is therefore not allowed access.

After some research it was said that i can use JSONP or CORS. I was able to use JSONP however, I am getting a js error because the API supports only both JSON & XML and not JSONP. Now I am trying to convert my ajax call to CORS but I simply can't understand the syntax. I always get the same error about the 'Access-Control-Allow-Origin'.

I hope someone can help me translate this one. Thanks.

  • 写回答

3条回答 默认 最新

  • weixin_33744854 2015-05-14 05:33
    关注

    Access-Control-Allow-Origin should be in the response header. It sounds the server from which you're trying to get the data did not enable CORS (set a response header of Access-Control-Allow-Origin). If this is the case I don't think you'll be able to get your resource through Javascript. You'll need to do it from the backend.

    评论

报告相同问题?

悬赏问题

  • ¥15 关于#python#的问题:求帮写python代码
  • ¥20 MATLAB画图图形出现上下震荡的线条
  • ¥15 LiBeAs的带隙等于0.997eV,计算阴离子的N和P
  • ¥15 关于#windows#的问题:怎么用WIN 11系统的电脑 克隆WIN NT3.51-4.0系统的硬盘
  • ¥15 来真人,不要ai!matlab有关常微分方程的问题求解决,
  • ¥15 perl MISA分析p3_in脚本出错
  • ¥15 k8s部署jupyterlab,jupyterlab保存不了文件
  • ¥15 ubuntu虚拟机打包apk错误
  • ¥199 rust编程架构设计的方案 有偿
  • ¥15 回答4f系统的像差计算