dongzhu7329 2016-03-22 12:53
浏览 173

使用ajax的API调用不起作用

I am using CI3 for one of my application. I do have some api created which is using different domain than that of application.

$.ajax({
        url: "http://www.example.com/restapi/index.php/api/user",
        type: "GET",
        data: {"user_id": user_id},
        username: "****",
        password: "****",
        success: function(response){

        },
        error: function(jqXHR, textStatus, errorThrown) {
            console.log(textStatus, errorThrown);
        },
        xhrFields: { withCredentials: true }
    });

When I call this api to get some data using jquery ajax I get error

NS_ERROR_DOM_BAD_URI: Access to restricted URI denied

so I get to know that due to different subdomain of my application and api I can not access it.

Is there any way to allow access to api of different subdomain. I am using authorization in API.

I don't want to call some php file in which I call that API using some php function, that make no sense. I want to call API directly.

Let me know way to do this and access API.

  • 写回答

1条回答 默认 最新

  • drau89457 2016-03-22 13:02
    关注

    You are breaking the same origin policy (https://www.w3.org/TR/cors)

    Sub domains, different ports, different protocols are considered different domains.

    <?php
      header("Access-Control-Allow-Credentials: true");
      header("Access-Control-Allow-Methods: GET, OPTIONS"); //Or post
      header("Access-Control-Allow-Origin: http://clientdomain");
    ?>
    

    Here more details for CI: https://github.com/chriskacerguis/codeigniter-restserver/issues/345

    评论

报告相同问题?

悬赏问题

  • ¥20 腾讯企业邮箱邮件可以恢复么
  • ¥15 有人知道怎么将自己的迁移策略布到edgecloudsim上使用吗?
  • ¥15 错误 LNK2001 无法解析的外部符号
  • ¥50 安装pyaudiokits失败
  • ¥15 计组这些题应该咋做呀
  • ¥60 更换迈创SOL6M4AE卡的时候,驱动要重新装才能使用,怎么解决?
  • ¥15 让node服务器有自动加载文件的功能
  • ¥15 jmeter脚本回放有的是对的有的是错的
  • ¥15 r语言蛋白组学相关问题
  • ¥15 Python时间序列如何拟合疏系数模型