[MV Plugin] Simple Ajax - Request data from a server

Plugin Name: Simple Ajax
Version: 1
Author: Amy Pond @ http://www.hbgames.org

Introduction

As RPG Maker MV games use JavaScript, one simple way to talk to a server is via the web, using Ajax. Technically here we aren't using the x part unless we format the responded text using XML, but the principle is the same.

This plugin is one simple function that returns a text variable from a web server using some entered variables.

How to Use

Create a PHP file on your web server which returns a text response (using echo() for example). Use GET variables to generate the response based on data sent to it.

There is an example PHP file in the Help part of the plugin.

Install the plugin, and type in the location of your PHP file, for example: http://www.rpgmakerweb.com/files/ajax.php (not a real file).

As an example of its usage:

var mytext = ajaxRequest("variable=1&othervariable=2");

On the server we check for $_GET['variable'] and $_GET['othervariable'].

We clean the variables before using (this is very important - please read up on cleaning user sent data in PHP).

Then we do something with these variables and send back some data. Say we echo("hello"), then:

var mytext = ajaxRequest("variable=1&othervariable=2"); // mytext = "hello"

  1. //=============================================================================
  2. // Ajax.js
  3. //=============================================================================
  4.  
  5. /*:
  6.  * @plugindesc A simple AJAX interface.
  7.  * @author Amy Pond @ http://www.HBGames.org. Created for Afar - the MMORPG
  8.  *
  9.  * @param URL
  10.  * @desc The URL to your actions php file.
  11.  * @default N/A
  12.  *
  13.  * @help To use this plugin you need a web server that can run PHP.
  14.  * On this server, host a PHP file.
  15.  * This file should check for GET variables, clean them (IMPORTANT),
  16.  * and return back a message using echo(). The function is called
  17.  * by ajaxRequest(parameters) where parameters takes the form:
  18.  *
  19.  * var1=data&var2=data&var3=data
  20.  *
  21.  * For example, I may wish to request the result of the equation 2+3.
  22.  * In game I call the function: ajaxRequest(action=add&number1=2&number2=3);
  23.  *
  24.  * Server side, using $_GET and cleaning it to an integer for security,
  25.  * I take these variables, add them together, and return the result:
  26.  *
  27.  * <?php
  28.  *   //action.php
  29.  *   switch($_GET['action'])
  30.  *   {
  31.  *     case 'add':
  32.  *       number1 = (int)$_GET['number1'];
  33.  *       number2 = (int)$_GET['number2'];
  34.  *       result = number1 + number2;
  35.  *       echo result;
  36.  *       break;
  37.  *     case 'login':
  38.  *       // code goes here
  39.  *       break;
  40.  *   }
  41.  * ?>
  42.  *
  43.  */
  44.  
  45. ajaxRequest = function(args) {
  46.  
  47.   var retrievedData = "";
  48.   var parameters = PluginManager.parameters('Ajax');
  49.   var get_params = args[0];
  50.  
  51.   // create a new Ajax request
  52.   var xhttp = new XMLHttpRequest();
  53.  
  54.   // This function is performed when a message is returned from the Ajax request
  55.   xhttp.onreadystatechange = function() {
  56.     if (xhttp.readyState == 4 && xhttp.status == 200) {
  57.       // set var retrievedData to the returned text
  58.       retrievedData = xhttp.responseText;
  59.     }
  60.   }
  61.  
  62.   // open the xhttp connection and send it
  63.   // this function is sync not async, such that the game will wait for the
  64.   // response. Change false to true and the code will run while the game
  65.   // does other things, so you don't have to pause for non-important things
  66.   // where the player doesn't need a response to continue, such as merely
  67.   // updating the online players list.
  68.   xhttp.open("GET", parameters['URL'] + "?" + get_params, false);
  69.   xhttp.send();
  70.  
  71.   // return the retrieved text
  72.   return retrievedData;
  73. };
Expand to see the code.

Credits/thanks

By Amy and originally created for the game Afar.

Thanks to w3schools.

Terms of Use

Free for use in any game with no credit necessary. If you like you can provide a link to HBGames.org or this thread but this is not necessary. Available for commercial games for free.

Please note that this script used incorrectly creates a vulnerable web server. If you do not clean your GET variables then you ARE open to hacking quite easily. No liability is accepted for any damage caused by use of this function. Use is at your own risk and I urge you to look into PHP security, in particular, cleaning data sent from a user. For integers this can be as simple as turning them into integer variables by (int)var, though for text you will need prewritten functions, or to make your own using preg_replace.

Original thread: https://www.hbgames.org/forums/viewtopic.php?t=78833