呼叫完成后不要返回axios呼叫

时间:2018-02-07 15:21:29

标签: php laravel vue.js axios

我正在尝试使用axios构建注册并遇到一些问题。 是否有可能在axios调用中将数据提交到后端并在将页面刷新到新的后立即返回到axios调用?

目前,我有这个解决方案:

    <!DOCTYPE html>
<html data-ng-app="myApp">

  <head>
    <script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/angularjs/1.6.7/angular.min.js"></script>
  </head>

  <body data-ng-controller="testController">

    <select id="state" ng-model="stateSrc" ng-options="state for (state, city) in states" ng-change="GetSelectedState()">
        <option value=''>State</option>
    </select>
    <select id="city" ng-model="citySrc" ng-options="city for (city,store) in stateSrc" ng-change="GetSelectedCity()" ng-disabled="!stateSrc">
        <option value=''>City</option>
    </select>
    <select id="city" ng-model="store" ng-options="store for store in citySrc" ng-disabled="!stateSrc || !citySrc">
        <option value=''>Store</option>
    </select>

    <script>
    angular
      .module('myApp', [])
      .run(function($rootScope) {
        $rootScope.title = 'myTest Page';
      })
      .controller('testController', ['$scope', function($scope) {
          $scope.states = {
            'STATE_1': {
              'City_1': ['Store_1', 'Store_2'],
              'City_2': ['Store_3', 'Store_4']
            },
            'STATE-2': {
              'City_3': ['Store_1', 'Store_2'],
              'City_4': ['Store_3', 'Store_4']
            }
          };

          $scope.GetSelectedState = function() {
            $scope.strState = $scope.stateSrc;
          };
          $scope.GetSelectedCity = function() {
            $scope.strCity = $scope.citySrc;
          };
        }
      ])
    </script>
  </body>
</html>

它在成功返回后显示成功消息,然后在一定时间后重定向,但我想要的是当调用返回直接将其发送到不同页面而不返回axios时,是否可能以某种方式?

我使用laravel作为后端,并使用axios / vue作为前端。

2 个答案:

答案 0 :(得分:1)

不,不可能。您需要存在then()函数来触发Axios调用的解析,从而触发该HTTP请求。

如果您不想延迟,只需删除6秒超时延迟?

答案 1 :(得分:1)

对于那些遇到类似问题的人,我现在发现的解决方案是在从axios重新启动后将所需数据保存到浏览器的会话存储中,并在页面重定向上显示消息:

重新加载前设置消息:

axios.post('/register', this.form.data)
 .then(function(response){
   sessionStorage.setItem("flashmessage", response.data.status); // status message
   window.location = response.data.redirect // route for redirection
}

获取消息并在页面重新加载时显示一次:

<script>
if (sessionStorage.flashmessage) {
  toastr.success(sessionStorage.flashmessage);
  sessionStorage.removeItem('flashmessage');
}
</script>

我使用了这里的信息: https://www.w3schools.com/jsref/prop_win_sessionstorage.asp