五、Ajax的使用
type:请求的类型;GET 或 POST
url:后端api接口链接
data:要传回后端的数据(传回变量必须与后端方法的参数名一样)
dataType:传输格式(一般为json)
success:function(data){}:data上面的不一样,自己定义的变量,储存后端传输过来的值
一、Ajax的查询(查询全部select)
//表单输入框输入默认对应值(一打开页面就加载)
window.onload=$.ajax({
type:'get',
url:'http://localhost:8080/goods/select',
data:{},
dataType:'json',
success:function(data){
console.log(data.data[0])
//给输入框绑定默认值
document.getElementById("id").value=data.data[0].id;
document.getElementById("img").value=data.data[0].img;
document.getElementById("imgsrc").value=data.data[0].imgsrc;
document.getElementById("exposition").value=data.data[0].exposition;
document.getElementById("explainsrc").value=data.data[0].explainsrc;
document.getElementById("price").value=data.data[0].price;
},
error:function (error){
console.log(error, '请求失败');
}
})
2、Ajax的修改(update)
$.ajax({ type:'get', url:'http://localhost:8080/goods/update', //传参回后端 data:{ id, img, imgsrc, exposition, explainsrc, price, }, dataType:'json', success:function(data){ }, error:function (error){ console.log(error, '请求失败'); } })