接口测试,ajax,post ,json data

<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <title>接口测试</title>
    <script src="https://code.jquery.com/jquery-1.11.3.js"></script>
</head>
<body>
<table>
<tr>
<td style="text-align:right">url:</td>
<td><textarea cols="40" rows="10" id="urlData">http://localhost:9415/api/paymentservice/payapply/shsocialinsurance</textarea></td>
</tr>
<tr>
<td style="text-align:right">request Data:</td>
<td><textarea cols="40" rows="10" id="reqData" >{businessType:1}</textarea></td>
</tr>
<tr>
<td style="text-align:right">response Data:</td>
<td id="responseDataTd"></td>
</tr>
<tr>

<td colspan="2"><input type="button" value="测试" οnclick="doTest()"></td>
</tr>
</table>




<script>
function doTest(){
$.ajax({
  url: $("#urlData").val(),
  data: $("#reqData").val(),
  type: "POST",
  dataType: "json",
          contentType : "application/json;charse=UTF-8",
  success: function( result ) {
    if(result.code == 0 ){
    $("#responseDataTd").css("color","green");
}else{
$("#responseDataTd").css("color","red");
}

$("#responseDataTd").html(JSON.stringify(result));
  },
  error:function(a,b,c){
    $("#responseDataTd").css("color","red");
$("#responseDataTd").html("a:"+JSON.stringify(a)+",b:"+JSON.stringify(b)+",c:"+JSON.stringify(c));
  }
});
}
</script>
</body>

</html>


成功:


失败:


  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
首先,我们需要使用前端的 AJAX 技术来从后端项目中获取商品信息。下面是一个示例代码: ```html <!DOCTYPE html> <html> <head> <title>商品页</title> <meta charset="utf-8"> <script src="https://cdn.bootcdn.net/ajax/libs/jquery/3.6.0/jquery.min.js"></script> </head> <body> <table id="goodsTable"> <thead> <tr> <th>ID</th> <th>商品名称</th> <th>价格</th> <th>规格</th> <th>简介</th> </tr> </thead> <tbody></tbody> </table> <script> // 使用 AJAX 获取商品信息 $.get('http://10.8.12.150:8088/ajaxtest/goods', function(data) { // 将得到的 JSON 数据转换为 JavaScript 对象 var goodsList = JSON.parse(data); // 遍历商品数组,生成 HTML 代码并添加到表格中 goodsList.forEach(function(goods) { var html = '<tr>' + '<td>' + goods.id + '</td>' + '<td>' + goods.goodsName + '</td>' + '<td>' + goods.price + '</td>' + '<td>' + goods.spec + '</td>' + '<td>' + goods.about + '</td>' + '</tr>'; $('#goodsTable tbody').append(html); }); }); </script> </body> </html> ``` 在这个示例中,我们使用了 jQuery 库来简化 AJAX 的操作。我们使用 $.get 方法来发送 GET 请求,得到的数据会自动转换为字符串类型的 JSON 数据。我们通过 JSON.parse 方法将其转换为 JavaScript 对象,并遍历商品数组,生成 HTML 代码并添加到表格中。 注意,由于跨域访问的限制,我们需要确保后端项目支持跨域访问。如果不支持,我们可以使用 JSONP 或者代理等技术来解决跨域问题。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值