我想要根据发送请求返回的结果进行选择,但是前后端200正常,就是不进入success、error、complete中
function isBuy(id) {
var goodId = id;
layer.open({
type: 1
, title: '购买提示'
, offset: '100px'
, content: '<input id=\"goods_id\" type="number" style=\"padding: 20px 100px; color: deepskyblue\"></input>'
, btn: ['确定', '取消']
, btnAlign: 'c' //按钮居中
, shade: 0 //不显示遮罩
, yes: function () {
var goodsId = $("#goods_id").val();
$.ajax({
type: "post",
url: "/addCart?id=" + goodId + "&goods=1&goodId=" + goodsId,
async:false,
dataType: "json",
success: function (jsonResult) {
alert("成功");
},
error: function () {
alert("加入购物车失败");
},
complete: function () {
alert("执行完成");
}
});
alert("完成!");
layer.closeAll();
parent.location.reload();
return 0;
}
, btn2: function () {
layer.close();
return 0;
}
});
}
@RequestMapping("/addCart")
@ResponseBody
public JSONResult cart(String id, String goods, String goodId, HttpSession session) {
return JSONResult.ok("加入成功!");
// JSON格式的通用响应对象,封装的就是后台返回给前台的所有信息
}
用这个试试 如果不行加 不行再header:{"content-type":"application/x-www-form-urlencoded"}
function isBuy(id) {
var goodId = id;
layer.open({
type: 1
, title: '购买提示'
, offset: '100px'
, content: '<input id=\"goods_id\" type="number" style=\"padding: 20px 100px; color: deepskyblue\"></input>'
, btn: ['确定', '取消']
, btnAlign: 'c' //按钮居中
, shade: 0 //不显示遮罩
, yes: function () {
var goodsId = $("#goods_id").val();
$.ajax({
type: "post",
url: "/addCart",
async:false,
data:{
id:goodId,
goods:1,
goodId: goodsId
},
dataType: "json",
success: function (jsonResult) {
alert("成功");
},
error: function () {
alert("加入购物车失败");
},
complete: function () {
alert("执行完成");
}
});
alert("完成!");
layer.closeAll();
parent.location.reload();
return 0;
}
, btn2: function () {
layer.close();
return 0;
}
});
}
async: false
改为async: true
或者删除
确定下是不是get方式,是get的话,将type: "post"改成type: "GET"
检查下dataType跟服务器是否一样