使用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.

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