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?

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... :)

commented: Thanks for sharing! +15
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.