设为首页收藏本站同能贴吧 切换语言 繁体中文
开启辅助访问 切换到窄版
扫描二维码关注官方公众号
返回列表
+ 发新帖
查看: 136|回复: 0

[转载发布] Simple Ajax - Request data from a web server

[复制链接]
累计送礼:
0 个
累计收礼:
1 个
  • TA的每日心情
    开心
    2026-7-12 04:10
  • 签到天数: 209 天

    连续签到: 2 天

    [LV.7]常住居民III

    9072

    主题

    864

    回帖

    6万

    积分

    管理员

    Rank: 9Rank: 9Rank: 9

    VIP
    7
    卡币
    58885
    OK点
    16
    推广点
    0
    同能卷
    50
    积分
    68851

    灌水之王

    发表于 2026-7-26 17:51:29 | 显示全部楼层 |阅读模式
    Plugin Name: Simple Ajax

    Version: 1

    Author: Amy Pond @ 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"

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


    Credits/thanks

    By Amy Pond of www.hbgames.org 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.


    本贴来自国际rpgmaker官方论坛作者:Ellie Jane处,因国际论坛即将永久关站,为了存档多年珍贵资料,署名转载到本论坛存档,由于官方帖子为英文原帖,需要中文翻译请点击论坛顶部切换语言为中文就可以将帖子翻译成中文浏览,方便大家随时查看,原文地址:https://forums.rpgmakerweb.com/threads/simple-ajax-request-data-from-a-web-server.48720/
    天天去同能,天天有童年!
    回复 送礼论坛版权

    使用道具 举报

    文明发言,和谐互动
    文明发言,和谐互动
    高级模式
    您需要登录后才可以回帖 登录 | 立即注册

    本版积分规则

    简体中文
    繁體中文
    English(英语)
    日本語(日语)
    Deutsch(德语)
    Русский язык(俄语)
    بالعربية(阿拉伯语)
    Türkçe(土耳其语)
    Português(葡萄牙语)
    ภาษาไทย(泰国语)
    한어(朝鲜语/韩语)
    Français(法语)
    关闭

    幸运抽奖

    社区每日抽奖来袭,快来试试你是欧皇还是非酋~

    立即查看

    聊天机器人
    Loading...

    QQ|Archiver|手机版|小黑屋|同能RPG制作大师 ( 沪ICP备12027754号-3 )

    GMT+8, 2026-8-22 08:38 , Processed in 0.136750 second(s), 52 queries .

    Powered by Discuz! X3.4

    Copyright © 2001-2020, Tencent Cloud.

    快速回复 返回顶部 返回列表