doubaoxue5788 2012-01-21 01:48
浏览 36
已采纳

使用JQuery无形地调用URL并返回成功/错误标志

I am coding a web based game in which the player moves around on a 7x7 grid. Currently, the move buttons call a ../action/move/up style PHP url that updates the database and then reloads the game page to reflect the new player position.

I want to be able to have the player click move and use JQuery tweens to move the player token without a page refresh. The part I'm unsure about is, I want this to update the server and then lock the move buttons. The JS would then call a URL or two that would return new values for a few variables on the page, as well as a flag checking if the move was successful. If it was, the move buttons would then unlock and allow the player to move again.

The pseudocode I'm imagining:

On Click (Move Up)  
{  
if(ReadyToMove)
    {
    Tween(ship, old position, new position)
    ReadyToMove = 0
    Call MoveDone
    }
}

MoveDone
{
ReadyToMove = Url('../movecheck/movement/' . current position .)
/* this url would return either 1 or 0 depending on if db xy matches given xy */    

if(!ReadyToMove)
{ current position = old position; display error(movefail) }

}

How do I actually do this?

  • 写回答

1条回答 默认 最新

  • dongzantai7570 2012-01-21 01:50
    关注

    Have a look at $.ajax():

    $.ajax({
        method: 'POST',
        url: ':./movecheck/movement/'
        data: /* an object containing data key/value pairs */,
        dataType: 'json',
        success: function(resp) {
            /* do stuff after the response from the server has been received */
        }
    });
    

    To get a proper resp object, simply echo json_encode(...); with ... being an associative array or stdClass object containing your data, e.g. array('canMove' => true)

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 关于#windows#的问题:怎么用WIN 11系统的电脑 克隆WIN NT3.51-4.0系统的硬盘
  • ¥15 matlab有关常微分方程的问题求解决
  • ¥15 perl MISA分析p3_in脚本出错
  • ¥15 k8s部署jupyterlab,jupyterlab保存不了文件
  • ¥15 ubuntu虚拟机打包apk错误
  • ¥199 rust编程架构设计的方案 有偿
  • ¥15 回答4f系统的像差计算
  • ¥15 java如何提取出pdf里的文字?
  • ¥100 求三轴之间相互配合画圆以及直线的算法
  • ¥100 c语言,请帮蒟蒻写一个题的范例作参考