How do I retrieve data using AJAX over cross-domain

Get help using Construct 2

Post » Wed Apr 22, 2015 3:32 pm

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

Code: Select all
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');


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

Image

Here is the event sheet (.capx is absolutely massive!)

Image

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: Select all
{
    "c2array": true,
    "size": [
        1,
        5,
        1
    ],
    "data": [
        "50",
        "test",
        "1",
        "4442da43afe442558e6d28fdc68c4a8d",
        1429718276
    ]
}


Any help would be greatly appreciated.

Thanks
B
4
Posts: 4
Reputation: 210

Post » Wed Apr 22, 2015 3:57 pm

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.
Who dares wins
B
57
S
17
G
21
Posts: 1,878
Reputation: 19,572

Post » Wed Apr 22, 2015 4:23 pm

Problem solved! Thanks so much.
B
4
Posts: 4
Reputation: 210


Return to How do I....?

Who is online

Users browsing this forum: No registered users and 25 guests