vue中的ajax

   <script>
        new Vue({
            el: '#app',
            data: {
                isShow1: true,
                isShow2: false,
                isShow3: false,
                token: "{{csrf_token()}}",
                start_time: 6,
                end_time: 22,
                notice_start_time: [
                    {
                        "indexId": 0,
                        "name": "00:00"
                    }, {
                        "indexId": 1,
                        "name": "01:00"
                    }, {
                        "indexId": 2,
                        "name": "02:00"
                    }, {
                        "indexId": 3,
                        "name": "03:00"
                    }, {
                        "indexId": 4,
                        "name": "04:00"
                    }, {
                        "indexId": 5,
                        "name": "05:00"
                    }, {
                        "indexId": 6,
                        "name": "06:00"
                    }, {
                        "indexId": 7,
                        "name": "07:00"
                    }, {
                        "indexId": 8,
                        "name": "08:00"
                    }, {
                        "indexId": 9,
                        "name": "09:00"
                    }, {
                        "indexId": 10,
                        "name": "10:00"
                    }, {
                        "indexId": 11,
                        "name": "11:00"
                    }, {
                        "indexId": 12,
                        "name": "12:00"
                    }, {
                        "indexId": 13,
                        "name": "13:00"
                    }, {
                        "indexId": 14,
                        "name": "14:00"
                    }, {
                        "indexId": 15,
                        "name": "15:00"
                    }, {
                        "indexId": 16,
                        "name": "16:00"
                    }, {
                        "indexId": 17,
                        "name": "17:00"
                    }, {
                        "indexId": 18,
                        "name": "18:00"
                    }, {
                        "indexId": 19,
                        "name": "19:00"
                    }, {
                        "indexId": 20,
                        "name": "20:00"
                    }, {
                        "indexId": 21,
                        "name": "21:00"
                    }, {
                        "indexId": 22,
                        "name": "22:00"
                    }, {
                        "indexId": 23,
                        "name": "23:00"
                    },],
                notice_end_time: [
                    {
                        "indexId": 0,
                        "name": "00:00"
                    }, {
                        "indexId": 1,
                        "name": "01:00"
                    }, {
                        "indexId": 2,
                        "name": "02:00"
                    }, {
                        "indexId": 3,
                        "name": "03:00"
                    }, {
                        "indexId": 4,
                        "name": "04:00"
                    }, {
                        "indexId": 5,
                        "name": "05:00"
                    }, {
                        "indexId": 6,
                        "name": "06:00"
                    }, {
                        "indexId": 7,
                        "name": "07:00"
                    }, {
                        "indexId": 8,
                        "name": "08:00"
                    }, {
                        "indexId": 9,
                        "name": "09:00"
                    }, {
                        "indexId": 10,
                        "name": "10:00"
                    }, {
                        "indexId": 11,
                        "name": "11:00"
                    }, {
                        "indexId": 12,
                        "name": "12:00"
                    }, {
                        "indexId": 13,
                        "name": "13:00"
                    }, {
                        "indexId": 14,
                        "name": "14:00"
                    }, {
                        "indexId": 15,
                        "name": "15:00"
                    }, {
                        "indexId": 16,
                        "name": "16:00"
                    }, {
                        "indexId": 17,
                        "name": "17:00"
                    }, {
                        "indexId": 18,
                        "name": "18:00"
                    }, {
                        "indexId": 19,
                        "name": "19:00"
                    }, {
                        "indexId": 20,
                        "name": "20:00"
                    }, {
                        "indexId": 21,
                        "name": "21:00"
                    }, {
                        "indexId": 22,
                        "name": "22:00"
                    }, {
                        "indexId": 23,
                        "name": "23:00"
                    },],
                is_send: 0,
                order_time: 30,
                order_end_time_id: 30,
                order_end_time: [
                    {
                        "indexId": 0,
                        "name": "0分钟"
                    }, {
                        "indexId": 5,
                        "name": "5分钟"
                    }, {
                        "indexId": 15,
                        "name": "15分钟"
                    }, {
                        "indexId": 30,
                        "name": "30分钟"
                    }, {
                        "indexId": 60,
                        "name": "1小时"
                    }, {
                        "indexId": 120,
                        "name": "2小时"
                    }, {
                        "indexId": 180,
                        "name": "3小时"
                    }, {
                        "indexId": 240,
                        "name": "4小时"
                    }
                ],
                message_list:[],
                page:1,
                message_statistics:[],
                message_number:0,
                page_list:[],
                totle_page:0,
                shop_name:""
            },
            created: function () {
                this.get_shop_expediting();
                //this.get_page_list();
            },
            methods: {
                toggle1: function () {
                    this.isShow1 = true
                    this.isShow2 = false
                    this.isShow3 = false
                },
                toggle2: function () {
                    this.isShow1 = false
                    this.isShow2 = true
                    this.isShow3 = false
                },
                toggle3: function () {
                    this.isShow1 = false
                    this.isShow2 = false
                    this.isShow3 = true
                },
                get_shop_expediting: function () {
                    this.$http.post('/order/get_shop_expediting', {}, {emulateJSON: true})
                        .then(function (response) {
                            //alert(response.body.message);
                            let get_data = response.body.expediting;
                            //console.log(response.body);
                            this.start_time = get_data.notice_start_time
                            this.end_time = get_data.notice_end_time
                            this.is_send = get_data.is_send
                            this.order_end_time_id = get_data.order_time
                            this.message_list =response.body.message_data
                            this.message_statistics =response.body.message_statistics
                            this.message_number =response.body.message_number
                            this.get_page_list_number(this.message_number);
                            this.shop_name=response.body.shop_info.shop_name
                            console.log(this.message_statistics);
                            console.log(response.body);
                        });

                },
                get_page_list_number:function (number) {
                    if(number <= 10 ){
                        this.page_list =[{ "page":1 }];
                    }else{
                        for(let i =1 ;i<= Math.ceil(number/10)  ;i++){
                            this.page_list[this.page_list.length]= {"page":i}
                        }
                    }
                    this.totle_page =this.page_list.length;
                    console.log( "========");
                    console.log( this.page_list);

                },
                get_page:function (page) {
                    this.$http.post('/order/index_data', {"page":page}, {emulateJSON: true})
                        .then(function (response) {
                            let get_data = response.body.message_data;
                            if(get_data.length>0 && page > this.page){
                                this.page =this.page+1 ;
                            }else{
                                this.page =this.page-1
                            }
                            this.message_list=get_data ;

                        });
                },
                //催付基本设置
                base_edit_data: function () {
                    var postdata = {
                        token: this.token,
                        notice_start_time: this.start_time,
                        notice_end_time: this.end_time,
                        is_send: this.is_send,
                        order_time: this.order_end_time_id,
                    };
                    this.$http.post('/order/expediting_base_edit', postdata, {emulateJSON: true})
                        .then(function (response) {
                            //alert(response.body.message);
                            $("#changeSetUp").hide();
                            $("#basicSetUp").show();
                        });

                },
                //短信设置
                message_edit_data: function () {
                    var postdata = {
                        _token: this.data.token,
                        message_content: null,
                        test_mobile: null,
                    };
                    this.$http.post('/order/expediting_message_edit', postdata, {emulateJSON: true})
                        .then(function (response) {

                        });
                    // $("#changeSetUp").hide();
                    //$("#basicSetUp").show();
                },
                //短信测试发送设置
                message_send: function () {
                    var postdata = {
                        _token: this.data.token,
                        message_content: null,
                        test_mobile: null,
                    };
                    this.$http.post('/order/test_send_message', postdata, {emulateJSON: true})
                        .then(function (response) {

                        });
                },
                //状态更新
                update_status_edit: function () {
                    var postdata = {
                        _token: this.data.token,
                        open_status: null, //  0 或者 1
                    };
                    this.$http.post('/order/expediting_update_status_edit', postdata, {emulateJSON: true})
                        .then(function (response) {

                        });
                }
            }
        })
    </script>

### 如何在 Vue 中使用原生 XMLHttpRequest 进行 Ajax 请求 在 Vue.js 应用程序中可以直接利用 `XMLHttpRequest` 对象发起 AJAX 调用来执行异步操作。下面是一个简单的例子展示怎样创建并发送 GET 请求: ```javascript new Vue({ el: '#app', data() { return { message: '' } }, mounted() { const xhr = new XMLHttpRequest(); xhr.open('GET', 'https://api.example.com/data'); xhr.onreadystatechange = function () { if (xhr.readyState === 4 && xhr.status === 200) { this.message = JSON.parse(xhr.responseText).message; } }.bind(this); xhr.send(null); // 发送请求,对于 GET 方法此参数为空即可 } }) ``` 为了处理 POST 请求或者更复杂的场景比如上传文件,则需要调整上述代码中的某些部分以适应特定需求。 当涉及到表单提交特别是带有文件的数据时,应该构建一个 `FormData` 实例来容纳这些信息,并将其作为 payload 提交给服务器[^4]。 另外值得注意的是,在现代 Web 开发实践中,通常推荐采用 Fetch API 或者 Axios 库替代原始的 `XMLHttpRequest` 接口因为它们提供了更加简洁易读且功能强大的接口设计[^1]。 尽管如此,了解如何运用基础组件仍然是很有价值的知识点之一。 #### 处理响应与错误情况 除了成功回调外,还需要考虑可能出现的各种异常状况,例如网络连接失败或是服务端返回非预期的状态码等问题。因此建议加入 try-catch 结构以及相应的 error handler 来增强健壮性。 ```javascript try { ... } catch(e){ console.error('Request failed:', e); } finally{ // 清除加载状态或其他清理工作 } ``` 通过这种方式不仅可以捕获潜在的风险还能给用户提供更好的体验提示。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值