【问题标题】:GET and POST issue in laravel chat using Pusher and Vue.js使用 Pusher 和 Vue.js 在 laravel 聊天中的 GET 和 POST 问题
【发布时间】:2018-05-04 22:23:33
【问题描述】:

我在按照本教程使用 Pusher 和 Vue.js 在 Laravel 中实现简单聊天时遇到问题:link tutorial

首先,我在导航栏中的路线是这条:

http://localhost/youChat/public/

我的 web.php 文件包含以下路径:

Auth::routes();
Route::get('/', 'TweetController@index');
Route::get('tweets', 'TweetController@showTweets')->middleware('auth');
Route::post('tweets', 'TweetController@sentTweet')->middleware('auth');

我在 assets/js 中提出请求的 app.js 文件是这个:

const app = new Vue({
el: '#app',

data: {
    tweets: []
},
created() {
    this.showTweets();
    Echo.private('chat')
        .listen('TweetSentEvent', (e) => {
        this.tweets.push({
            tweet: e.tweet.tweet,
            user: e.user
 });
 });
 },
methods: {
    showTweets() {
        axios.get('/tweets').then(response => {
            this.tweets = response.data;
        });
    },
     addTweet(tweet) {
        this.tweets.push(tweet);

        axios.post('/tweets', qs.stringify(tweet)).then(response => {
          console.log(response.data);
        });
    }
}
});

如您所见,我使用 Axios 发送请求。

一切看起来都很好,但是 GET 和 POST 请求不起作用。控制台检查器中的错误显示如下:

GET http://localhost/tweets 404(未找到)

未捕获(承诺中)错误:请求失败,状态码为 404 在 createError (app.js:13931) 结算时 (app.js:35401) 在 XMLHttpRequest.handleLoad (app.js:13805)

获取https://stats.pusher.com/timeline/v2/jsonp/1session=Njg3NjQyNDY5NT....MjY1fV0%3D0() 发布http://localhost/broadcasting/auth 404(未找到)

当我尝试发帖时:

POST http://localhost/tweets 404(未找到)

get/post 应该朝着这个方向:

http://localhost/youChat/public/tweets

但我不知道发生了什么。有什么建议吗?我绝望了:D。 提前致谢!

【问题讨论】:

    标签: php laravel vue.js axios pusher


    【解决方案1】:

    您收到此错误是因为您使用的是绝对路径。

    因此,您可以将 Base url 存储在变量中,也可以使用相对路径

    这是一个例子。

    methods: {
        showTweets() {
            axios.get('tweets').then(response => {
                this.tweets = response.data;
            });
        },
         addTweet(tweet) {
            this.tweets.push(tweet);
    
            axios.post('tweets', qs.stringify(tweet)).then(response => {
              console.log(response.data);
            });
        }
    }
    

    删除网址前的/

    保存一个

    const URL = '{{url('/')}}'

    methods: {
        showTweets() {
            axios.get(URL + '/tweets').then(response => {
                this.tweets = response.data;
            });
        },
         addTweet(tweet) {
            this.tweets.push(tweet);
    
            axios.post(URL + '/tweets', qs.stringify(tweet)).then(response => {
              console.log(response.data);
            });
        }
    }
    

    希望对你有帮助

    【讨论】:

    • 工作得很好:D 非常感谢你,伙计!删除破折号解决了这个问题。
    猜你喜欢
    • 2021-05-19
    • 2020-11-05
    • 2018-01-19
    • 2019-11-25
    • 2013-11-08
    • 2016-09-18
    • 2015-05-17
    • 2019-03-18
    • 2016-07-23
    相关资源
    最近更新 更多