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

Get help using Construct 2

Post » Fri Nov 14, 2014 5:20 pm

I'm trying to send an AJAX post request to a php page to be handled using the following action.
Image
The request returns as completed and triggers the following event.
Image

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?
Nothing goes unanswered!
B
24
S
5
G
1
Posts: 434
Reputation: 3,446

Post » Sat Nov 15, 2014 5:44 pm

It says in the documentation of the ajax object
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:
Code: Select all
<?php
   $postdata = $_POST['data'];
   
   echo("postdata: ");
   if(isset($postdata)) {
      echo ($postdata);
   } else {
      echo ("post data not set");
   }
?>
Nothing goes unanswered!
B
24
S
5
G
1
Posts: 434
Reputation: 3,446

Post » Sat Nov 15, 2014 6:23 pm

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.
B
55
S
29
G
19
Posts: 1,520
Reputation: 25,670

Post » Sat Nov 15, 2014 6:40 pm

I'm not trying to test from the preview. I have now got a set up like so:
Image
Code: Select all
<?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");
   }
?>


This is the alert:
postdata: post data not set, getdata: gettest
Nothing goes unanswered!
B
24
S
5
G
1
Posts: 434
Reputation: 3,446

Post » Sat Nov 15, 2014 6:55 pm

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)
B
55
S
29
G
19
Posts: 1,520
Reputation: 25,670

Post » Sat Nov 15, 2014 6:58 pm

ah, okay, i'll give it a shot

EDIT: Problem solved, thanks!!!
Nothing goes unanswered!
B
24
S
5
G
1
Posts: 434
Reputation: 3,446

Post » Mon Apr 27, 2015 4:47 pm

briggybros.. could you post the solution that worked for you ? I am faced with similar issue and keep hitting the 404 msg.
B
6
Posts: 16
Reputation: 296

Post » Mon Apr 27, 2015 8:50 pm

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.
Nothing goes unanswered!
B
24
S
5
G
1
Posts: 434
Reputation: 3,446


Return to How do I....?

Who is online

Users browsing this forum: No registered users and 9 guests