我正在使用CI3作为我的一个应用程序。 我确实创建了一些api,它使用的域名与应用程序不同。
$.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 }
});
当我使用jquery ajax调用此api获取一些数据时,我收到错误
NS_ERROR_DOM_BAD_URI: Access to restricted URI denied
所以我知道由于我的应用程序和api的不同子域我无法访问它。
有没有办法允许访问不同子域的api。 我在API中使用授权。
我不想调用一些我使用某些php函数调用该API的php文件,这没有任何意义。我想直接调用API。
让我知道这样做并访问API。
答案 0 :(得分:0)
您违反了相同的原始政策(https://www.w3.org/TR/cors)
子域,不同端口,不同协议被视为不同的域。
<?php
header("Access-Control-Allow-Credentials: true");
header("Access-Control-Allow-Methods: GET, OPTIONS"); //Or post
header("Access-Control-Allow-Origin: http://clientdomain");
?>
此处有关CI的详细信息:https://github.com/chriskacerguis/codeigniter-restserver/issues/345