前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >javascript jquery ajax动态提交多个参数 api测试

javascript jquery ajax动态提交多个参数 api测试

作者头像
用户1258909
发布2018-07-03 12:10:36
1.5K0
发布2018-07-03 12:10:36
举报
文章被收录于专栏:拂晓风起拂晓风起

为了方便自己测试rest api,所以做了一个动态参数的页面。大家有需要的话,就各取所需吧。

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <title>API测试</title>
        <script type="text/javascript" src="../script/jquery-1.5.2.min.js"></script>
        <script type="text/javascript" src="../script/jquery.autocomplete.min.js"></script>

        <link rel="stylesheet" href="../script/jquery.autocomplete.css" type="text/css" />

        <script type="text/javascript">
           
            function addOne() {
                $("#diy").append('<p>参数名:<input type="text" class="parameterName"/> 参数值:<input type="text" class="parameterValue"/></p>');
            }
            
            function submitForm() {
                var returnData = document.getElementById("phpRadio").checked ? "php":"json";
                var method = document.getElementById("getRadio").checked ? "get":"post";
                var action = "../api/"+$("#action").val();
                alert(action)
                var formData = new Object();
                var allParameters = $(".parameter");
                for(var i=0;i<allParameters.length;i++){
                    formData[allParameters[i].name] = allParameters[i].value;
                }
                var allDiyParameterNames = $(".parameterName");
                var allDiyParameterValues = $(".parameterValue");
                for(var i=0;i<allDiyParameterNames.length;i++){
                    formData[allDiyParameterNames[i].value] = allDiyParameterValues[i].value;
                }
                if(method == 'get'){
                  $.get(action, formData, function(data){
                      //
                  }, returnData);
          }
            }
        </script>
    </head>
    <body>
        <div  style="float:left;" >
            <form id="form1" method="post" target="_blank" onsubmit="action1()" enctype="multipart/form-data">
                <p>类/方法:<input type="text" id="action"/>如:user/show</p>
                <p>传递方法:<input type="radio" id="getRadio" name="getPost" checked="true" /><label for="getRadio">GET </label>
                    <input type="radio" name="getPost" id="postRadio"/><label for="postRadio">POST</label></p>
                <p>返回数据格式:<input type="radio" id="phpRadio" name="returnData" checked="true" /><label for="phpRadio">php </label>
                    <input type="radio" name="returnData" id="jsonRadio"/><label for="jsonRadio">json</label></p>
                <p>sig:<input type="text" class="parameter" name="sig" />
                    id:<input type="text"  class="parameter" name="id" /></p>
                <p>sid:<input type="text"  class="parameter" name="sid" />
                    mid:<input type="text"  class="parameter" name="mid" /></p>
                <div id="diy">
                    <input type="button" onclick="addOne()" style="width:100px;height: 30px;" value="添加字段"/>
                </div>

                <input type="button" onclick="submitForm()" style="width:200px;height: 30px;" value="提交"/>
                <input type="reset" style="width:100px;height: 30px;" value="清空"/>
            </form>
        </div>
        <div style="float:right;">
            返回结果:<br><textarea id="result" rows="10" style="width:500px;"></textarea>
        </div>
    </body>
</html>
本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2011-08-28 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档