简体   繁体   English

如何在Axios + Vue Component + Laravel中设置根目录以修复javascript中的404 POST

[英]How to set root directory to fix 404 POST in javascript for Axios + Vue Component + Laravel

I'm following a tutorial to learn Vue+Laravel and it uses Axios for the Ajax request on these lines in the script for the Vue Component. 我正在遵循一个教程来学习Vue + Laravel,并在Vue组件脚本的这些行上将Axios用于Ajax请求。 The error causing me grief is on the console log is: 导致我悲伤的错误是在控制台日志上:

    POST http://localhost/favorite/2 404 (Not Found)

my website lives locally on wamp at a different folder which looks like the issue. 我的网站在wamp上的本地文件夹中,看起来像问题所在。 I can't use the normal laravel "Url(' ') helper function to get the route directory. So how would I do this in javascript rather just hardcoding my local host site in which isn't good for pushing to a real server in the future. Thanks! 我无法使用常规的laravel“ Url('')帮助器函数来获取路由目录。因此,我将如何在javascript中进行此操作,而只是对本地主机站点进行硬编码,而这对于将其推送到真实服务器中是没有好处的。未来,谢谢!

http://localhost/laravel/public/ http:// localhost / laravel / public /

        methods: {
        favorite(post) {
            axios.post('/favorite/'+post)
                .then(response => this.isFavorited = true)
                .catch(response => console.log(response.data));
        },

        unFavorite(post) {
            axios.post('/unfavorite/'+post)
                .then(response => this.isFavorited = false)
                .catch(response => console.log(response.data));
        }
    }

Create a new axios instance when the component is mounted: 安装组件后,创建一个新的axios实例:

mounted() {
    this.axios = axios.create({
        baseURL: 'http://localhost',
    });
}

You could even go ahead an store the base uri in a data attribute: 您甚至可以将基本uri存储在data属性中:

data() {
    baseUri: 'http://localhost',
},

mounted() {
    this.axios = axios.create({
        baseURL: this.baseUri,
    });
}

Then later in your methods you can make use of this.axios . 然后,在以后的方法中,您可以使用this.axios See the docs for reference. 请参阅文档以供参考。

Cheers! 干杯!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM