Ajax往后台传参数,无参数,一个参数,多个参数,一个对象等

原文:http://www.cnblogs.com/chenwolong/p/Get.html

//无参数请求-简单示例
$(document).ready(function () {
$.ajax({
url: "http://localhost:9953/api/Person/Get",
type: "post",
contentType: "application/json",
dataType: "text",
data:{},
success: function (result,status) {
if (status == "success") {
alert(result);
}
},
error: function (error) {
alert(error);
}
});
});
//单个参数传递- data: {"":"3"}, 这种方式也竟然正确
$(document).ready(function (data) {
$.ajax({
url: "http://localhost:9953/api/Person/GetById",
type: "post",
contentType: "application/json",
dataType: "text",
data: {Id:"3"},
success: function (result,status) {
alert(result)
},
error: function (error) {
alert(error);
}
});
});

 

 

转载于:https://www.cnblogs.com/ZkbFighting/p/8651536.html

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
可以使用以下步骤来实现ajax提交据到后台并跳转到新的jsp页面,同时携带多个参数: 1. 在前端页面中使用ajax提交据到后台jsp页面,例如: ``` $.ajax({ type: "POST", url: "backend.jsp", data: { name: "John", age: 30 }, success: function(response) { // 成功提交后的操作 var result = JSON.parse(response); // 将后台返回的json字符串转换为对象 var id = result.id; var status = result.status; window.location.href = "newPage.jsp?id=" + id + "&status=" + status; // 跳转到新的jsp页面并携带参数 }, error: function() { // 提交失败后的操作 window.location.href = "error.jsp"; // 跳转到错误页面 } }); ``` 2. 在后台jsp页面中获取前端提交的据,并根据需要进行处理,例如: ``` String name = request.getParameter("name"); int age = Integer.parseInt(request.getParameter("age")); // 进行据处理 ... int id = 123; String status = "success"; JSONObject json = new JSONObject(); // 创建json对象 json.put("id", id); // 添加属性值 json.put("status", status); response.getWriter().write(json.toJSONString()); // 将json对象转换为字符串并返回给前端页面 ``` 3. 在新的jsp页面中获取携带的参数并使用,例如: ``` <% int id = Integer.parseInt(request.getParameter("id")); String status = request.getParameter("status"); // 使用参数进行页面展示或其他操作 %> ``` 注意事项: - 在前端页面中使用ajax提交据时,要注意设置请求类型和提交的据格式。 - 在后台jsp页面中获取提交的参数时,要注意参数的名称和类型。 - 在后台jsp页面中返回据时,可以使用json格式,便于前端页面处理。 - 在新的jsp页面中获取携带的参数时,要注意参数的名称和类型。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值