ajax 设置同步请求

版权声明:欢迎转评,一起学习. https://blog.csdn.net/xiaobinqt/article/details/84258846

ajax 是异步的,但是在某些特殊的情况下,需要 ajax 支持同步请求,即先执行完 ajax 请求再执行下面的代码.

可以设置 

 async: false 

实现这个功能.示例代码

$.ajax({
                            url: "/api/goods-number-is-exist?number=" + number + "&goods_id=" + goods_id,
                            type: 'get',
                            contentType: 'application/json',
                            dataType: 'text',
                            async: false, // 同步
                            success: function (result) {
                                var resObj = JSON.parse(result);
                                // console.log("ddd");
                                switch (resObj.code) {
                                    case "-1":
                                        alert(resObj.message);
                                        break;
                                    case "1":
                                        number_exist = true;
                                        break;
                                    default:
                                        break;
                                }
                            },
                            error: function (result) {
                            }
                        });

猜你喜欢

转载自blog.csdn.net/xiaobinqt/article/details/84258846
今日推荐