我正在用angularJs建造laravel 5.1.

当用户点击一个按钮时,我想发送一个destroy request以将其从数据库中删除,然后在完成后发送一个get request以获取新数据,因为其中一个已被删除.

So I attached my method to an ng-click event on a button, this works, it hits the method.

I then run a .destroy request. Inside the .then() method of that .destroy I want to then call another method which has a .get request.

这在Safari中非常有效,但在Chrome或Firefox中不起作用.

Here is my code for the controller, the method that is called on button click to delete is deleteOpportunity():

$scope.getOpportunities = function()
    {
        UBOService.get()
            .then(function successCallback(responsed) {
                $scope.opportunities = responsed.data;
            }, function errorCallback(response) {
                $scope.error = response;
            });
    }
$scope.deleteOpportunity = function()
    {

                UBOService.destroy($scope.activeItem.id)
                    .then(function successCallback(response) {
                        $scope.getOpportunities();

                        return false;
                    }, function errorCallback(response) {
                        $scope.error = response;
                    });

    }

My service code:

app.service('UBOService', function($http) {

    return {
        get : function() {
            return $http.get('/api/user-booked-opportunities');
        },

        destroy : function(id) {

            return $http.delete('/api/user-booked-opportunities/' +  id);
        }
    }
})

Am I doing something wrong? Is there something I am missing? How does Safari interact differently with this code which makes it work?

推荐答案

很难从你发布的参数来衡量,但仅仅基于你说这在Safari中非常有效,但在Chrome或Firefox中不起作用,这听起来可能是CORS的问题.

Firefox and Chrome have different requirements for cross-origin requests than Safari. Is your Laravel api endpoint for this destroy action located at the same location as your Angular app? What Access-Control-Allow-Origin header is the API returning?

try 向Laravel添加以下内容,看看它是否能使该块在这些浏览器中保持一致:

App::before(function($request) {
  // Enable CORS 
  // In production, replace * with http://yourdomain.com 
  header("Access-Control-Allow-Origin: *");
  header('Access-Control-Allow-Credentials: true');

  if (Request::getMethod() == "OPTIONS") {
    // The client-side application can set only headers allowed in Access-Control-Allow-Headers
    $headers = [
      'Access-Control-Allow-Methods' => 'POST, GET, OPTIONS, PUT, DELETE',
      'Access-Control-Allow-Headers' => 'X-Requested-With, Content-Type, X-Auth-Token, Origin, Authorization'
    ];
    return Response::make('You are connected to the API', 200, $headers);
  }
});

(^ source)

Laravel相关问答推荐

为什么删除查询执行 Laravel

如何获取多态多对多关系子查询的自动 id 列名?

如何自定义密码确认不匹配.错误信息?

不能在 laravel 中使用控制器名称,必须使用命名空间

安卓 retrofit |发布自定义对象(将 json 发送到服务器)

Homestead 2.0 多个站点,都链接到同一个 url

Laravel Eloquent Pluck 不丢失密钥

使用 vue-router 更改路由时中止所有 Axios 请求

Laravel 5.1 重命名项目

Database[] 未配置 Laravel 5

如何更改 Handlebars.js 的默认分隔符?

如何在 Laravel 5.2 中手动发送密码重置请求?

Laravel 应用程序中应用程序键的意义是什么?

Laravel db 迁移 - renameColumn 错误 - 请求了未知的数据库类型枚举

Laravel Mail 发送Electron邮件但返回 false

Eloquent 模型不更新 updated_at 时间戳

Laravel 仓库

如何让 Laravel 将 View 的Content-Type标头返回为application/javascript?

如何在不要求用户登录 Laravel 的情况下验证Electron邮件

Laravel Mix:更新 Node.js 依赖项