CodeIgniter Forums
Codeigniter: AJAX fails to access the controller: Access-Control-Allow-Origin - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Using CodeIgniter (https://forum.codeigniter.com/forumdisplay.php?fid=5)
+--- Forum: General Help (https://forum.codeigniter.com/forumdisplay.php?fid=24)
+--- Thread: Codeigniter: AJAX fails to access the controller: Access-Control-Allow-Origin (/showthread.php?tid=67240)



Codeigniter: AJAX fails to access the controller: Access-Control-Allow-Origin - henry_spike - 01-30-2017

I was working on a login system using Codeigniter. I tried to use AJAX to submit POST data to a controller.

Take a look at http://www.henryspike.tk/udemy

When we click on Submit, the AJAX does not access the Controller and chrome returns the error:

Code:
XMLHttpRequest cannot load http://www.henryspike.tk/udemy/index.php/main/login. No 'Access-Control-Allow-Origin' header

is

present on the requested resource. Origin 'http://henryspike.tk' is therefore not allowed access. The response had HTTP

status code 500.

Please note that I installed Codeigniter in a sub directory of henryspike.tk i.e Udemy

So as both the Codeigniter files and views and everything is on the same domain, why is it giving the Cross Domain error? 

Is it because I installed Codeigniter in a sub directory? I would like to state that I edited the base url correctly to http://www.henryspike.tk/udemy in my config.php


RE: Codeigniter: AJAX fails to access the controller: Access-Control-Allow-Origin - spjonez - 01-31-2017

You're on your naked domain (no www) and making an AJAX request to www which are technically not the same subdomain. Make them both the same and you won't receive that error.


RE: Codeigniter: AJAX fails to access the controller: Access-Control-Allow-Origin - vasileios.city - 01-31-2017

I do not receive any http error but the below:

<h4>A PHP Error was encountered</h4>
<p>Severity: Notice</p>
<p>Message: Undefined property: CI_DB_mysqli_result::$row</p>
<p>Filename: models/Users_model.php</p>
<p>Line Number: 9</p>


RE: Codeigniter: AJAX fails to access the controller: Access-Control-Allow-Origin - InsiteFX - 01-31-2017

Looking at your page source code there is no form action code to tell it where to go.

You can pass CodeIgniter base_url to Ajax using the below:

Place in your html head tag

Code:
    <!-- jQuery baseUrl -->
    <script>
        var baseUrl = "<?php echo base_url(); ?>";
        var siteUrl = "<?php echo site_url(); ?>";
    </script>

Now you can access it in Ajax using baseUrl or siteUrl.


RE: Codeigniter: AJAX fails to access the controller: Access-Control-Allow-Origin - saurabh123 - 03-11-2018

yes this is working properly... thanks..