How do I correctly format data in an AJAX post request?

0 favourites
  • 8 posts
From the Asset Store
We present to you “Post Apocalyptic Trailer” – our newest hard hitting, bass rumbling designed movie trailer collection.
  • I'm trying to send an AJAX post request to a php page to be handled using the following action.

    The request returns as completed and triggers the following event.

    To here it all seems to work fine, however, when creating the alert, AJAX.LastData doesn't return the output from the page. When using API Kitchen to test it manually, it works as intended, simply returning the value passed in with the key 'data'. This leads me to believe that the way I have formatted the data of the POST is incorrect. How am I supposed to pass a key and its value to a POST in C2?

  • It says in the documentation of the ajax object [quote:115ktyol]Note post data is in the same format as a query string, e.g. "foo=1&bar=2".

    which is the way I have formatted my post data.

    My php file:

    <?php
    	$postdata = $_POST['data'];
    	
    	echo("postdata: ");
    	if(isset($postdata)) {
    		echo ($postdata);
    	} else {
    		echo ("post data not set");
    	}
    ?>[/code:115ktyol]
  • Check in the browser dev tools. It will show the exact request it's trying to make and any error. If you're trying to make the AJAX request from C2 preview you probably need to enable Cross-Origin Resource Sharing on the server.

  • I'm not trying to test from the preview. I have now got a set up like so:

    <?php
    	$postdata = $_POST['postdata'];
    	$getdata = $_GET['getdata'];
    	
    	echo("postdata: ");
    	if(isset($postdata)) {
    		echo ($postdata .", ");
    	} else {
    		echo ("post data not set, ");
    	}
    	
    	echo("getdata: ");
    	if(isset($getdata)) {
    		echo($getdata);
    	} else {
    		echo("get data not set");
    	}
    ?>[/code:1kyp5bdb]
    
    This is the alert:
    [quote:1kyp5bdb]postdata: post data not set, getdata: gettest
  • Ah do it without the URLEncode. You don't want to encode the "=". If you're sending user entered data then just encode the actual values like:

    "postdata=" & URLEncode(data)

  • Try Construct 3

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

    Try Now Construct 3 users don't see these ads
  • ah, okay, i'll give it a shot

    EDIT: Problem solved, thanks!!!

  • briggybros.. could you post the solution that worked for you ? I am faced with similar issue and keep hitting the 404 msg.

  • 404's are not to do with this, the problem I was having was that I was encoding the = in my post data to it's URL friendly form. A 404 is when the page cannot be retrieved; have you checked that the URL you're posting to is correct? Try to visit it in your browser to check.

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