[Solved] How do I Retrieve Data with AJAX JSON

Get help using Construct 2

Post » Sat Mar 15, 2014 3:32 am

Hello all,

I am a experienced web developer. I created a AJAX event to a URL when the event is trigger the AJAX error function gets called. I checked the network movement with Chrome and I see it heading to the right URL but it's getting 301 headers (Moved permanently)(Also I'm not sure if this is a bug but the call gets repeated about every half a second and it froze my Chrome browser). I open the url it is pointing to (copied it from the network box) and I'm getting a 200 OK message. Any clue what is going on? I checked my page output and this is all that is there
Code: Select all
[["Andrew",90],["John",80],["Austin",70]]

No HTML, is that the problem?
Last edited by OzairP on Sat Mar 15, 2014 8:13 pm, edited 1 time in total.
B
4
Posts: 10
Reputation: 220

Post » Sat Mar 15, 2014 2:59 pm

No, it's not a problem. I've created a few RESTful web services which return bare JSON and successfully consumed them in Construct with the AJAX requests.

Can you show us the event sheet where you request the url and process returned data?
B
4
Posts: 6
Reputation: 262

Post » Sat Mar 15, 2014 3:50 pm

If you are previewing through localhost, you will not get any response from your web server, because "By default, browsers block AJAX requests across domains.".

As for "(Also I'm not sure if this is a bug but the call gets repeated about every half a second and it froze my Chrome browser)" check how your event is fired.

Hopefully that is the case with localhost, but since you're experienced, I'm not sure, heh.
ImageImageImageImage
B
62
S
19
G
51
Posts: 633
Reputation: 30,836

Post » Sat Mar 15, 2014 3:55 pm

If you are adressing a PHP page with the Ajax call, add:

header('Access-Control-Allow-Origin: *');

to your PHP page.
Who dares wins
B
57
S
17
G
21
Posts: 1,878
Reputation: 19,572

Post » Sat Mar 15, 2014 4:12 pm

Or just use this: https://chrome.google.com/webstore/deta ... ljbi?hl=en

Works wonders. I activate it every time I want to test my AJAX requests. Because if previewing in localhost I don't think you can add "header('Access-Control-Allow-Origin: *'); " or at least I was confused. :X
ImageImageImageImage
B
62
S
19
G
51
Posts: 633
Reputation: 30,836

Post » Sat Mar 15, 2014 4:58 pm

@DatapawWolf

I use it all the time when dealing with php.

For previewing, and also when exported through cocoonjs and likes ;)
Who dares wins
B
57
S
17
G
21
Posts: 1,878
Reputation: 19,572

Post » Sat Mar 15, 2014 5:13 pm

lennaert wrote:For previewing, and also when exported through cocoonjs and likes ;)
@lennaert Oh! How do you do that within a preview? Like if I just press "Run Layout" which files would I modify?
ImageImageImageImage
B
62
S
19
G
51
Posts: 633
Reputation: 30,836

Post » Sat Mar 15, 2014 5:23 pm

Simply in the PHP file, add the bit of code.

header('Access-Control-Allow-Origin: *');

As soon as the device which does the Ajax request (PC:browser or mobile:app) does not have the same IP as the server serving the PHP file, then its cross domain.


generally adding the above code to your php file eliminates all cross domain issues.
Who dares wins
B
57
S
17
G
21
Posts: 1,878
Reputation: 19,572

Post » Sat Mar 15, 2014 5:46 pm

@lennaert ooooh, no I get it now. Yeah, no I'm using another company's API, and also you cannot preview in your browser and use AJAX calls without you using access control to enable a response back. I think that's what @OzairP's issue is as well.

I've probably butchered the terms but that's what I was having issues with, my browser was not letting the response back from the website, so I used that app above to enable responses and it worked.
ImageImageImageImage
B
62
S
19
G
51
Posts: 633
Reputation: 30,836

Post » Sat Mar 15, 2014 7:46 pm

@lennaert @DatapawWolf

I added the header to my PHP file, but I've found the origin of error. I checked the JavaScript console and it showed this error.
Code: Select all
XMLHttpRequest cannot load http://ozairpatel.com/api/game/asa/gettopscores. No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http://localhost:50000' is therefore not allowed access.


I've uploaded my files and It returned 200 OK, but it would be nice to run it locally, any other suggestions?

EDIT: Okay, so I've just resorted to running my apache server locally and having the ajax url point to localhost, thanks for the help though!
B
4
Posts: 10
Reputation: 220


Return to How do I....?

Who is online

Users browsing this forum: No registered users and 3 guests