http_api.html 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. <include file="Common/header" />
  2. <style type="text/css">
  3. input{
  4. width: 100%;
  5. }
  6. </style>
  7. <div id="layout" style="width: 70%;margin: auto;">
  8. <form class="form-horizontal">
  9. <h2 style="text-align: center;">GET和POST测试</h2>
  10. <div class="control-group">
  11. <div class="controls">
  12. <label >接口地址(如:http://www.abc.com/api/login)</label>
  13. <select style="width: 100px;" id="method" >
  14. <option value="post">POST</option>
  15. <option value="get">GET</option>
  16. </select>
  17. <input type="text" id="url" placeholder="接口地址" >
  18. </div>
  19. </div>
  20. <div class="control-group">
  21. <div class="controls">
  22. <label >参数(如:user_id=121&age=22&date=2016-06-02)</label>
  23. <input type="text" id="params" placeholder="参数" >
  24. </div>
  25. </div>
  26. <div class="control-group">
  27. <div class="controls">
  28. <button type="button" class="btn btn-primary" id="http-submit">提交</button>
  29. <button type="reset" class="btn btn-link" >清除</button>
  30. </div>
  31. </div>
  32. <div class="control-group">
  33. <div class="controls">
  34. <label >返回结果</label>
  35. <textarea style="width: 100%;height: 300px;" id="http-response"></textarea>
  36. </div>
  37. </div>
  38. </form>
  39. </div>
  40. <include file="Common/footer" />
  41. <script type="text/javascript">
  42. $(function () {
  43. //格式化json数据
  44. function dump(arr,level) {
  45. var dumped_text = "";
  46. if(!level) level = 0;
  47. //The padding given at the beginning of the line.
  48. var level_padding = "";
  49. for(var j=0;j<level+1;j++) level_padding += " ";
  50. if(typeof(arr) == 'object') { //Array/Hashes/Objects
  51. var i=0;
  52. for(var item in arr) {
  53. var value = arr[item];
  54. if(typeof(value) == 'object') { //If it is an array,
  55. dumped_text += level_padding + "\"" + item + "\" : \{ \n";
  56. dumped_text += dump(value,level+1);
  57. dumped_text +=level_padding +"\}";
  58. } else {
  59. dumped_text += level_padding + "\"" + item + "\" : \"" + value + "\"";
  60. }
  61. if(i<Object.getOwnPropertyNames(arr).length-1){
  62. dumped_text+=", \n";
  63. }else{
  64. dumped_text+=" \n";
  65. }
  66. i++;
  67. }
  68. } else { //Stings/Chars/Numbers etc.
  69. dumped_text = "===>"+arr+"<===("+typeof(arr)+")";
  70. }
  71. return dumped_text;
  72. }
  73. $("#http-submit").on('click',function () {
  74. $.post("?s=home/page/ajaxHttpApi",{
  75. url:$("#url").val(),
  76. method:$("#method").val(),
  77. params:$("#params").val()
  78. },function (data) {
  79. console.log(JSON.parse(data));
  80. var text="\{ \n"+dump(JSON.parse(data))+" \}";//整体加个大括号
  81. $("#http-response").val(text);
  82. });
  83. });
  84. });
  85. </script>