首页 > 解决方案 > 多次执行AJAX请求时如何删除以前的数据

问题描述

我对 ajax 调用有疑问。它工作正常,除了一件事,当我尝试使用相同选项获取数据多次返回新响应但仍返回前一个响应的数据时。

我认为我错过了一些东西。

ajax 脚本

$('#search').on('click', function () {
    var date = $("#date").val();
    $.ajax({
           type: 'GET',
           url: '{{Route("dashboard.status")}}',
           
            data: {
                    date: date
                 },
           dataType: "JSon",
           success: function(response){
             console.log(response.manager_hourlog);
                // Employee  report script
              var colors = ["#1abc9c", "#2ecc71", "#3498db", "#9b59b6", "#34495e", "#16a085", "#27ae60", "#2980b9", "#8e44ad", "#2c3e50", "#f1c40f", "#e67e22", "#e74c3c", "#ecf0f1", "#95a5a6", "#f39c12", "#d35400", "#c0392b", "#bdc3c7", "#7f8c8d"];
                  @if ($auth->user_type != 1) 
                  // manager  report script
                  var managerchartbar = {
                    labels: response.manager_projects,
                    datasets: 
                [{ 
                  
                        label: response.users,
               backgroundColor: colors[Math.floor(Math.random() * colors.length)],
                        data: response.totals    
                    },]
                };     
                    var ctx = document.getElementById('manager').getContext('2d');
                    window.myBar = new Chart(ctx, {
                      type: 'bar',
                      data: managerchartbar,
                      options: {
                        title: {
                          display: true,
                          text: 'Project Report chart'
                        },
                        tooltips: {
                          mode: 'index',
                          intersect: false
                        },
                        responsive: true,
                        scales: {
                          xAxes: [{
                            stacked: true,
                          }],
                          yAxes: [{
                            stacked: true
                          }]
                        }
                      }
                    });
                  @endif
              },
          error: function(xhr){
            console.log(xhr.responseText);
          }});
            });
          });
    </script>

标签: ajaxlaravel

解决方案


您应该将您的方法更改为 POST 以获取 json 请求/响应 API,这样会更安全并避免 laravel 缓存查看它。

type: 'POST',

尝试将方法更改为 POST(对您的 api 服务器和路由执行相同操作)。

如果不起作用,请显示您的 laravel API 代码。


推荐阅读