Laravel + Vue + Axios 问题 POST 方法
Laravel + Vue + Axios issue with POST method
我正在做一个 Laravel 5.6 项目,它存储在 VPS 上(我们称之为“生产”,尽管没有这样的创建环境)。
我们还结合了 Plesk 和 Github 手动将 Web 应用程序从我们的本地环境部署到服务器。
问题 是当我从 API 加载一些数据时 return 错误 405 方法不允许 (GET)... 但它们实际上在 app.js
和 routes/api.php
.
中注册为 POST
最棒的是,它们在我的本地环境中运行完美。
这里有一些信息:
服务器:
- Ubuntu 服务器 14.04
- 阿帕奇/MySQL
- PHP 7.2.5
我的电脑:
- Windows 10 与 XAMPP
- 阿帕奇/MySQL
- PHP 7.2.2
每个浏览器中的 Developer 工具:
Request Method: GET
Status Code: 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 请求 return 一切正常,所以 API 似乎在 POST 请求上工作正常。
尝试删除 url 中的尾部斜杠。
例如,
/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;
}
},
我遇到了这个问题,我通过使计算属性具有完整的 url 来解决它
computed: {
sendAnswersUrl: function () {
return window.location.protocol + "//" + window.location.hostname, + "/api/properties/countries";
}
}
然后 post 使用 axios
axios.post(this.sendAnswersUrl, {
group: this.id,
})
.then(data => {})
.catch(() => {});
我正在做一个 Laravel 5.6 项目,它存储在 VPS 上(我们称之为“生产”,尽管没有这样的创建环境)。
我们还结合了 Plesk 和 Github 手动将 Web 应用程序从我们的本地环境部署到服务器。
问题 是当我从 API 加载一些数据时 return 错误 405 方法不允许 (GET)... 但它们实际上在 app.js
和 routes/api.php
.
最棒的是,它们在我的本地环境中运行完美。
这里有一些信息:
服务器:
- Ubuntu 服务器 14.04
- 阿帕奇/MySQL
- PHP 7.2.5
我的电脑:
- Windows 10 与 XAMPP
- 阿帕奇/MySQL
- PHP 7.2.2
每个浏览器中的 Developer 工具:
Request Method: GET
Status Code: 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 请求 return 一切正常,所以 API 似乎在 POST 请求上工作正常。
尝试删除 url 中的尾部斜杠。
例如,
/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;
}
},
我遇到了这个问题,我通过使计算属性具有完整的 url 来解决它
computed: {
sendAnswersUrl: function () {
return window.location.protocol + "//" + window.location.hostname, + "/api/properties/countries";
}
}
然后 post 使用 axios
axios.post(this.sendAnswersUrl, {
group: this.id,
})
.then(data => {})
.catch(() => {});