How do I post data using AJAX?

  • I saw a tutorial on reading data from a web server, but nothing on saving data to an online server.

    I tried using the AJAX Post To URL but I keep getting an error. (I am uploading my build to the same server so crossdomain security shouldn't be the issue)

    All I am trying to do is to upload some text from a form to a text file. From that I hope then to be able to upload images too. The Construct 2 form bit is easy enough, I just need to know to know what the AJAX parameters should be and/or what PHP do I need to call?

    Cheers

  • Construct 2 Event:

    Button : "on clicked" => AJAX : Request URL ( "http://myserver.com/test.php?myVar=myContent)

    PHP Script (test.php):

    <?php

    file_put_contents("data.txt", $_GET['myVar']);

    ?>

  • Cool, but i still cant get it working. (It still errors on the server)

    My path is:

    "http://myserver.com/test.php?myVar="&URLEncode(TextBox.Text)

    Is something wrong with that?

    Cheers

  • Hm, what is your exact error?

    Your data.txt have to be created once before you run your script. Just upload an empty "data.txt" to your server.

  • Try Construct 3

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

    Try Now Construct 3 users don't see these ads
  • Yeah, I already have a data.txt file there (in the same folder as the test.php file) but it doesn't update.

    I don't have any error information other than I am receiving an AJAX:On error message.

  • It should work.

    Try to run your php script with your browser to check for errors.

    You can also upload your .capx file so i could take a look at it.

  • Here is the dropbox link to the file. The php on it's own works fine.

    dropbox.com/s/nnpq4aixls3hyz6/NoUpload.capx

  • You dont need the "&" after myVar, but that is not the error.

    So your capx looks right. Test the following

    Open the URL pigobo.com/UploadTest/test.php

    After that check your "data.txt". If it dosen't contain "TEST1234" your PHP Script don't work correctly or you dont have permission to write to the file.

  • The data file does contain the TEST1234 string so I do have permission and the PHP is fine.

    When I try the same with Construct 2 calling the php file it doesn't work. I even removed the & but still no success.

  • Maybe a cross-domain security error ;-)

    Whats the URL you uploaded your exported html and js files?

  • The html file is on myserver.com/Uploader01 and the php file is in myserver.com/UploadTest.

    Can't see why there would be a security issue. DO they need to be in the same folder too?

  • Nope they only have to be at the same domain.

    In my current project all AJAX Requests work fine and i'm sorry but i can't figure out where your problem could be. We already checked the most relevant parts.

  • okay thanks. I'll keep looking and post here when I figure it out.

    Thanks for your help!

  • Do you use Firefox with an Addon like "Request Policy" or some crazy restrictive firewall-rules ?

    Capx ... checked

    PHP Script ...checked

    Server config and permission ... checked

    So the last thing we have not checked is your Browser / System ^^

    Good luck finding the error <img src="smileys/smiley2.gif" border="0" align="middle" />

  • Hmm, I'm using Chrome & no weird extensions. :)

    For some reason even the php file has stopped working although I haven't changed it.

    Does it matter that I am using the free trial version?

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