AJAX?

Get help using Construct 2

Post » Thu Dec 15, 2011 10:01 pm

Can someone help me out please, Im trying to execute this API. it can be used with multiple languages, Including javascript and PHP, so im trying to use it, because surely it works with construct2 right?

But when using the ajax, i test for a error, and i get one. Nothing specific, just using the AJAX on error event, set text to 'error'.

Well here is the PHP -
// Submit those variables to the server
$post_data = array(
    'api_key' => 'f2eb0260b6073a40f4b0b128a66709aaebbeb754',
    'game_id' => 'Rf2x0WpI8',
    'response' => 'JSON'
);

// Send a request to example.com
$result = post_request('https://www.scoreoid.com/api/getNotification', $post_data);

if ($result['status'] == 'ok'){

    // Print headers
    echo $result['header'];

    echo '<hr />';

    // print the result of the whole request:
    echo $result['content'];

}
else {
    echo 'A error occured: ' . $result['error'];
}


I Also tried to use the javascript version, using the callJS plugin, But I do not know what to call, $.post?


$.post("https://www.scoreoid.com/api/getNotification", { api_key: "f2eb0260b6073a40f4b0b128a66709aaebbeb754", game_id: "Rf2x0WpI8", response: "JSON"},
   function(data) {
     alert("Data Loaded: " + data);
   },
"json"
);
B
29
S
12
G
7
Posts: 740
Reputation: 7,839

Post » Thu Dec 15, 2011 11:24 pm

First, does your php script works?
Do you get the header and content from https://www.scoreoid.com/api/getNotification as expected?

If yes, I think you just have to do a
[code]System: start of layout
    -> AJAX: Request"http://yourdomain/blahblah/yourphpscript.php" (tag:"call")
AJAX: On "call" completed
    -> Text: set text to AJAX.LastData
AJAX: On "call" error
    -> Text: set text to "An error happened."[/code]
Also you might want to replace
[code]if ($result['status'] == 'ok')
{
    echo $result['header'];
    echo '<hr />';
    echo $result['content'];
}[/code]
by
[code]if ($result['status'] == 'ok')
{
    echo $result['header']."\n".$result['content'];
}[/code]
As Construct2 won't parse the HR element.Yann2011-12-15 23:25:35
B
60
S
22
G
14
Posts: 1,479
Reputation: 16,346

Post » Fri Dec 16, 2011 3:01 am

$.post is jQuery, so make sure you include the jQuery.js file, if you want to use it :)

Have you tried testing the JS in a seperate HTML file yet? I also highly recommend to use a browser (Chrome)/extension (Firebug for Firefox) with a console, so that you can see what the AJAX post returns to you.Fryght2011-12-16 03:03:02
B
10
G
2
Posts: 5
Reputation: 1,227

Post » Fri Dec 16, 2011 2:18 pm

The output is still error..
B
29
S
12
G
7
Posts: 740
Reputation: 7,839


Return to How do I....?

Who is online

Users browsing this forum: anty21ro, DamonZero, Radulepy and 1 guest