How do I retrieve data using AJAX over cross-domain

  • Hi all,

    Pretty sure I've read every article, forum post and Stack Overflow piece regarding this problem, so you're my last hope!

    I'm trying to pull back some user data using the AJAX plugin, when I make the request through the Chrome extension Postman, I get the correct data back, but through the Construct debug view, I'm seeing all the signs of a cross-domain problem.

    Within the PHP file on the server (using the Codeigniter framework, if that makes a difference) I have the following headers set

    header('Access-Control-Allow-Origin: *');
    header('Access-Control-Allow-Methods: GET, PUT, POST, DELETE, OPTIONS');
    header('Access-Control-Max-Age: 1000');
    header('Access-Control-Allow-Headers: Content-Type, Authorization, X-Requested-With');
    header('Content-type: application/json');[/code:xls7b6dq] 
    
    I have even gone as far as to create a php file that just outputs the array statically, with those headers set. Nothing I do seems to work. Alerting the AJAX.LastData produces a blank box and I get the following console error on both Chrome and Firefox...
    
    [img="http://s2.postimg.org/50pewwd95/Capture1.png"]
    
    Here is the event sheet (.capx is absolutely massive!)
    
    [img="http://s17.postimg.org/s8jsgnb3j/Capture2.png"] 
    
    I've even gone as far as trying to set the request headers in the event sheet as a last act of desperation!
    
    The output of the array is as follows
    
    [code:xls7b6dq]{
        "c2array": true,
        "size": [
            1,
            5,
            1
        ],
        "data": [
            "50",
            "test",
            "1",
            "4442da43afe442558e6d28fdc68c4a8d",
            1429718276
        ]
    }[/code:xls7b6dq]
    
    Any help would be greatly appreciated.
    
    Thanks
  • Try Construct 3

    Develop games in your browser. Powerful, performant & highly capable.

    Try Now Construct 3 users don't see these ads
  • header('Access-Control-Allow-Origin: *');

    You only need that one in the top of your PHP file.

    header('Access-Control-Allow-Methods: GET, PUT, POST, DELETE, OPTIONS');

    header('Access-Control-Max-Age: 1000');

    header('Access-Control-Allow-Headers: Content-Type, Authorization, X-Requested-With');

    Remove these.

    Remove all the request header stuff in your events.

    Also, try setting a text object's text with the Ajax.LastData instead and remove the check if not 0.

  • Problem solved! Thanks so much.

Jump to:
Active Users
There are 1 visitors browsing this topic (0 users and 1 guests)