By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
434,795 Members | 1,235 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 434,795 IT Pros & Developers. It's quick & easy.

JQ/CodeIgniter App: CORS header 'Access-Control-Allow-Origin' does not match *

100+
P: 228
Hey Fellas,

I am seriously in despair. I have a CI driven app in domaina.com. I need to send cross domain request to a SINGLE controller in it from domainb.com. It will be an ajax call using POST method. It sends a single number and get a text reply from domaina.com.

htaccess is not really an option as I only call a single controller domaina.com/check (then a couple of methods in check controller). Here is what I have until now:

Expand|Select|Wrap|Line Numbers
  1.  public function __construct(){
  2.  
  3. header("Access-Control-Allow-Origin: domainb.com");
  4. header("Access-Control-Allow-Headers: X-API-KEY, Origin, X-Requested-With, Content-Type, Accept, Access-Control-Request-Method, XKMS,delayi");
  5. header("Access-Control-Allow-Methods: GET, POST, OPTIONS");
  6.  
  7. }
And in one of my methods, I do check the call is indeed ajax:

Expand|Select|Wrap|Line Numbers
  1.  
  2. function itemchecker() {
  3.  
  4.     $ajax = isset($_SERVER['HTTP_X_REQUESTED_WITH']) && $_SERVER['HTTP_X_REQUESTED_WITH'] == 'XMLHttpRequest';
  5.  
  6. if ($ajax) {
  7.   // handle specific Ajax differently...
  8.  echo "ajax called";
  9. //accept posted items and process now
  10. }
  11.  
  12.     }    
  13.  
  14.  
In domainb i have:

Expand|Select|Wrap|Line Numbers
  1.  var postForm = {'e':851470}; //Fetch form data
  2.  
  3.  
  4.  
  5.        $.ajax({
  6.                 type: "POST",
  7.                 url: "http://domaina.com/check/itemchecker/",
  8.  
  9.                 data: postForm,
  10.                 dataType : "text",
  11.  
  12.                 cache: "false",
  13.     contentType: "application/x-www-form-urlencoded",
  14.                 success: function (result) {
  15.  
  16.  
  17.                     alert(result);
  18.                 },
  19.                 fail: function (result){
  20.                 alert(result);
  21.                 }
  22.  
  23.  
  24.             });
  25.  
  26.  
  27.  
The exact error message I am getting now is:

Reason: CORS header 'Access-Control-Allow-Origin' does not match domainb.com

yet, i am on domainb.com. I did try adding http://domainb.com, domainb.com,domaib.com/page/action (which sends the request). Am really lost whats going on.

Any insight?
Jul 12 '16 #1
Share this Question
Share on Google+
1 Reply


gits
Expert Mod 5K+
P: 5,328
u might check what the jQuery request is sending as headers (for example in the chrome or firefox dev tools) - and try to set the:

Expand|Select|Wrap|Line Numbers
  1. Access-Control-Request-Method
  2. Access-Control-Request-Headers
headers explicitly for the request.
Jul 13 '16 #2

Post your reply

Sign in to post your reply or Sign up for a free account.