0

I am literary about to bleed...I have a single controller that will accept POST request from an external domain. Once the post data arrives (usually a number or a date), it returns json data. The problem is CORS error in firefoz and chrome.

Only that controller is going to be accessed from other domains. I have tried

header('Access-Control-Allow-Origin: *'); header('Access-Control-Allow- Methods: POST, GET, PUT, DELETE, OPTIONS'); header('Access-Control-Allow-Headers: X-Requested-With, content-type, X-Token, x-token');

just after opening the php code, in the constructor and even the method but nothing changes at all.

My current htaccess looks like this:

RewriteEngine on
RewriteCond $1 !^(index\.php|public|\.txt)
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?$1

Adding header files to it didn't solve the problem either.

The calling ajax is:

var postForm = {'e':8756321}; //id

   $.ajax({
            type: "POST",
            url: "http://domain.com/staff/checkid", //staff is controller

            data: postForm,
            dataType : "json",
            cache: "false",

            success: function (result) {

                alert(result[0]);
            },
            fail: function (result){

            }

        });

The codes work very well in non-codeigniter applications. Any universal solution that can work in all browsers and hopefully, don't need .htaccess?

3
Contributors
2
Replies
19
Views
2 Years
Discussion Span
Last Post by Rupsena
2

Your response headers are correct. Look for the extra space that you have added.

header('Access-Control-Allow-Origin: *');
header('Access-Control-Allow- Methods: POST, GET, PUT, DELETE, OPTIONS');
(You have a space -> Access-Control-Allow-(here)Methods)
header('Access-Control-Allow-Headers: X-Requested-With, content-type, X-Token, x-token');

I was looking for these exact thing and it worked perfectly for me(after removing the space).

Sorry for the late reply though.Cheers... :)

Votes + Comments
Thanks for sharing!
This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.