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

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • samvb
    New Member
    • Oct 2006
    • 228

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

    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:

    Code:
     public function __construct(){
    
    header("Access-Control-Allow-Origin: domainb.com");
    header("Access-Control-Allow-Headers: X-API-KEY, Origin, X-Requested-With, Content-Type, Accept, Access-Control-Request-Method, XKMS,delayi");
    header("Access-Control-Allow-Methods: GET, POST, OPTIONS");
    
    }
    And in one of my methods, I do check the call is indeed ajax:

    Code:
    function itemchecker() {
    
    	$ajax = isset($_SERVER['HTTP_X_REQUESTED_WITH']) && $_SERVER['HTTP_X_REQUESTED_WITH'] == 'XMLHttpRequest';
    
    if ($ajax) {
      // handle specific Ajax differently...
     echo "ajax called";
    //accept posted items and process now
    }
    
    	}
    In domainb i have:

    Code:
     var postForm = {'e':851470}; //Fetch form data
                
               
    
           $.ajax({
                    type: "POST",
                    url: "http://domaina.com/check/itemchecker/",
                  
                    data: postForm,
                    dataType : "text",
    
                    cache: "false",
    	contentType: "application/x-www-form-urlencoded",
                    success: function (result) {
    				
                       
    					alert(result);
                    },
    				fail: function (result){
    				alert(result);
    				}
    				
    				
                });
    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,dom aib.com/page/action (which sends the request). Am really lost whats going on.

    Any insight?
  • gits
    Recognized Expert Moderator Expert
    • May 2007
    • 5388

    #2
    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:

    Code:
    Access-Control-Request-Method
    Access-Control-Request-Headers
    headers explicitly for the request.
    Last edited by gits; Jul 13 '16, 03:52 PM.

    Comment

    Working...