首页 > 解决方案 > Laravel + Vue + Axios POST 方法的问题

问题描述

我正在开发一个存储在 VPS 上的 Laravel 5.6 项目(我们称之为“生产”,尽管没有这样的创建环境)。

我们还结合了 Plesk 和 Github,将 Web 应用程序从我们的本地环境手动部署到服务器。

问题是当我从 API 加载一些数据时,它们返回错误 405 Method not allowed ( GET )... 但它们实际上在和 in中注册为POSTapp.jsroutes/api.php

最棒的是,在我当地的环境中,它们可以完美运行。

这里有一些信息:

服务器:

我的电脑:

每个浏览器中的开发者工具:

请求方法: GET
状态码: 405 Method Not Allowed

这里是里面的代码app.js

loadCountries: function loadCountries(total) {
    axios.post('/api/properties/countries/').then(function (response) {
        app.countries = response.data;
    });

    total = total <= this.countries.length ? total : this.countries.length;

    if (total) {
        var newArr = [];
        for (i = 0; i < total; i++) {
            newArr.push(this.countries[i]);
        }
        this.countries = newArr;
    }
},

注意:如果我在开发人员工具中编辑相同的请求并再次发送它,但作为 POST 请求,它会返回一切正常,因此 API 在 POST 请求上似乎可以正常工作。

标签: phplaravelvue.jsaxiosplesk

解决方案


尝试删除网址中的尾部斜杠。

如,

/api/properties/countries

替换原来的那一行app.js会产生这个,

loadCountries: function loadCountries(total) {
axios.post('/api/properties/countries').then(function (response) {
    app.countries = response.data;
});

total = total <= this.countries.length ? total : this.countries.length;

if (total) {
    var newArr = [];
    for (i = 0; i < total; i++) {
        newArr.push(this.countries[i]);
    }
    this.countries = newArr;
}

},


推荐阅读