[Solved] How do I Retrieve Data with AJAX JSON

0 favourites
  • 6 posts
From the Asset Store
Explore and retrieve all Magical Books from the hidden Orc's Lair in this "PacMan-styled" game.
  • 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 adressing a PHP page with the Ajax call, add:

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

    to your PHP page.

  • Try Construct 3

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

    Try Now Construct 3 users don't see these ads
  • I use it all the time when dealing with php.

    For previewing, and also when exported through cocoonjs and likes

  • 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

    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)