1

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.

Jay
  • 821
  • 2
  • 18
  • 50
  • Have you tried getJSON method in jquery?Show your code – Mihai Mar 22 '16 at 12:55
  • You are running into the same origin policy. You could implement CORS or use [jsonp](https://learn.jquery.com/ajax/working-with-jsonp/). http://stackoverflow.com/questions/12296910/so-jsonp-or-cors – jtrumbull Mar 22 '16 at 13:05

1 Answers1

0

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

JP. Aulet
  • 4,375
  • 4
  • 26
  • 39
  • Try this to your server part: http://stackoverflow.com/questions/30920422/how-to-handle-custom-headers-with-cors-pre-flight-request-ajax-codeigniter .If still not work, try $.support.cors = true; in the frontend (take care: http://stackoverflow.com/questions/7852225/is-it-safe-to-use-support-cors-true-in-jquery ). Here another similar problem with the same solution: http://stackoverflow.com/questions/27186036/i18n-ns-error-dom-bad-uri-access-to-restricted-uri-denied – JP. Aulet Mar 22 '16 at 13:28