[Solved] How do I Retrieve Data with AJAX JSON

0 favourites
  • 10 posts
From the Asset Store
Data+ is the best Data Management solution for Construct 3. It contains 4 Addons (Plugin & Behavior).
  • 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

    [["Andrew",90],["John",80],["Austin",70]][/code:1y20b96b]
    No HTML, is that the problem?
  • 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?

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

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

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

    to your PHP page.

  • 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

  • DatapawWolf

    I use it all the time when dealing with php.

    For previewing, and also when exported through cocoonjs and likes

  • 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?

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

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

  • Try Construct 3

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

    Try Now Construct 3 users don't see these ads
  • 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.

    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. 
    [/code:3mal88kd]
    
    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!
Jump to:
Active Users
There are 1 visitors browsing this topic (0 users and 1 guests)