nodejs ajax 请求

前端之家收集整理的这篇文章主要介绍了nodejs ajax 请求前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

前端:

<center>

     <div id="form-playerinfo">
         <label for="game addr">服务器地址:</label>
         <input type="text" class="input" name="serverAddr" id="id_serverAddr" placeholder="请输入服务器地址" value="127.0.0.1">
         <label for="accountid">账号id:</label>
         <input type="text" class="input" name="nickname" id="id_nickname" placeholder="请输入账号ID" value="12345678">
         <label for="cmd">游戏命令:</label>
         <textarea  name="cmd" id="id_cmd" rows="10" cols="500" laceholder="additem"></textarea>
	 <p class="submit"><input type="button" id="id_submit" name="commit" onclick="submitdata()" value="确定修改"></p>
        </div>

<input value="" id="id_tip" disabled="true"  style="color:red;border: 0px solid white;">

</center>

<script type="text/javascript">


    function submitdata(){

        var params ={
            serverAddr: $("#id_serverAddr").val(),nickname: $("#id_nickname").val(),cmd:$("#id_cmd").val()
        };


        $.ajax({
            data: params,url: '/gm',type: 'post',dataType: 'json',async: false,cache: false,timeout: 5000,success: function(data){

                var datas = JSON.parse(data);
                if(200 == datas.code){
                    window.alert(datas.message);
                    //$("#id_tip").val(datas.message);
                }else{
                    window.alert(datas.message);
                   // $("#id_tip").val(datas.message);
                }
            },error: function(jqXHR,textStatus,errorThrown){
                window.alert('error ' + textStatus + " " + errorThrown);
            }
        });
    }

</script>
后端代码
router.post('/',function (req,res) {
    console.log("enter post");
	if (req.body.cmd != undefined && req.body.serverAddr != undefined && req.body.nickname != undefined) {
		
        var addrs = req.body.serverAddr.split(":")
        var cmds = req.body.cmd.split("\n")
	
        var strResult = {code:200,message:"执行成功,玮哥哥,看看效果吧"};
        strResult = JSON.stringify(strResult);

         res.json(strResult);
	}
      else
      {
        var strResult = {code:201,message:"执行失败额,玮哥哥"};
        strResult = JSON.stringify(strResult);

        res.json(strResult);
     }
});
原文链接:https://www.f2er.com/ajax/163244.html

猜你在找的Ajax相关文章