如何使用ajax(jquery)

以下是我第一次工作时写的ajax:

 1 $.ajax({
 2 
 3         url: "/spinweb/store/storeHome.do",
 4         dataType: 'json',
 5         data: {
 6             "pageSize": a
 7         },
 8         type: 'post',
 9 
10         success: function(data) {
11             console.log(data)
12             var pic = data.data
13             for(var i = 0; i < pic.length; i++) {
14                 if(pic[i]) {
15                     if(pic[i].head) {
16 
17                         $(".shop-list").eq(i).find("img").attr("src", imgUrl + pic[i].head)
18                     } else {
19                         $(".shop-list").eq(i).find("img").attr("src", "")
20                     }
21 
22                     $(".shop-list").eq(i).find(".shop-l-info").html(pic[i].businessstorename)
23                     $(".shop-list").eq(i).find(".storeid").html(pic[i].businessstoreid)
24 
25                 } else {
26                     $(".shop-list").eq(i).find("img").hide()
27                     $(".shop-list").eq(i).find(".shop-l-info").html("")
28                     $(".shop-list").eq(i).find(".storeid").html("")
29                 }
30 
31             }
32 
33             $(".shop-list").click(function() {
34 
35                 localStorage.shopid = $(this).find(".storeid").eq(0).text()
36                 localStorage.shopname = $(this).find(".shop-l-info").eq(0).text()
37 
38                 window.open("html/company-msg.html")
39             })
40 
41         },
42 
43     });

基本需要注意的有三点:

1.url

 这个是后端给的接口地址,必须正确书写。【并且,确定没有存在跨域问题】。

如果你的代码是在本地服务器上启动的,那么因为和公司服务器并不在一个域名上,所以无法访问。解决方法是将代码上传到公司服务器或者找找跨域软件解决。

 

2. data

这里可以放对象或者字符串两种格式都可以。但是推荐使用对象方式,因为当参数多的时候,不容易漏写或者多写。

 

3. 成功后返回的数据

当请求成功后,你会得到一组json数据如上面

 success: function(data)

的data。这些数据根据需要使用js或者jquery进行各种各样的作用,上图就是循环的将后台获得的商品信息填写到对应的位置。

转载于:https://www.cnblogs.com/thestudy/p/6273318.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值