前端jQuery发送Ajax异步请求

HTML代码:

<!--提交按钮-->
<div class="form-group">
    <label class="col-md-3 control-label"></label>
    <div class="col-md-8">
        <input id="btn-reg" class="btn btn-primary" type="button" value="立即注册" />
        <span class="pull-right">
            <small>已经有账号?</small><a href="login.html">登录</a>
        </span>
    </div>
</div>

JS代码:

<!--导入Cookie-->
<script src="../bootstrap3/js/jquery.cookie.js" type="text/javascript" charset="utf-8"></script>

$("#btn-login").click(function () {
    console.log($("#form-login").serialize())
    $.ajax({
        url: "/user/login",
        type: "POST",
        data: $("#form-login").serialize(),
        dataType: "json",
        success: function (json) {
            if (json.state == 200) {
                // alert("登录成功");
                $.cookie("avatar", json.data.avatar, {expires: 7})
                // 跳转
                location.href = "index.html";
            } else {
                alert("登录失败:" + json.msg);
            }
        },
        error: function (xhr) {
            alert("登录时产生了未知的错误异常:" + xhr.message);
        }
    });
});

页面加载后即执行:

$(document).ready(function () {
    $.ajax({
        url: "/user/get_by_id",
        type: "GET",
        data: $("#form-change-info").serialize(),
        dataType: "json",
        success: function (json) {
            if (json.state == 200) {

                $("#username").val(json.data.username)
                $("#phone").val(json.data.phone)
                $("#email").val(json.data.email)
                // $("#gender").val(json.data.gender)
                if (json.data.gender == 0) {
                    $("#gender-female").prop("checked", "checked")
                } else {
                    $("#gender-male").prop("checked", "checked")
                }


            } else {
                // alert("修改失败:" + json.msg);
            }
        },
        error: function (xhr) {
            // alert("修改时产生了未知的错误异常:" + xhr.status);
        }
    });
})

前端接收到Ajax的结果后,替换到页面上的JS代码:

$.ajax({
    url: "/product/hot_list",
    type: "GET",
    data: null,
    dataType: "json",
    success: function (json) {
        if (json.state == 200) {

            $("#hot-list").empty();

            let products = json.data;
            for (let i = 0; i < products.length; i++) {

                let div = "<div class=\"col-md-12\">\n" +
                    "    <div class=\"col-md-7 text-row-2\"><a href=\"product.html\">#{title}</a>\n" +
                    "    </div>\n" +
                    "    <div class=\"col-md-2\">#{price}</div>\n" +
                    "    <div class=\"col-md-3\"><img src=\"..#{image}collect.png\"\n" +
                    "                               class=\"img-responsive\"/></div>\n" +
                    "</div>"

                div = div.replace(/#{title}/g, products[i].title);
                div = div.replace(/#{price}/g, products[i].price);
                div = div.replace(/#{image}/g, products[i].image);

                $("#hot-list").append(div);
            }
        } else {
            alert("热销商品加载失败:" + json.msg);
        }
    },
    error: function (xhr) {
        alert("热销商品加载时产生了未知的错误异常:" + xhr.status);
    }
});

如何拿到网址地址处的key-value:

// http://localhost:8080/web/product.html?id=1001
let id=$.getUrlParam("id");

如果你的地址栏有很多的值:

// http://localhost:8080/web/orderConfirm.html?cids=6&cids=5
console.log(location.search.substr(1)); // cids=6&cids=5

前端要上传的数据的几种写法:

// 第一种
$("#form_id").serialize()

// 第二种
new FormData($("#form_id")[0]) // 只适用文件

// 第三种
"username=tom"

// 第四种
data:{
    "username":"tom"
    "age":18
}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

蒋劲豪

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值