duanfei8149 2018-07-08 02:43
浏览 46

通过刀片user.id路由来响应js组件

I need to pass the blade user.id to the react js component, however i keep getting

app.js:56717 POST http://127.0.0.1:8000/user/follow/undefined 419 (unknown status)

I followed a similar problem here, i got close, but it still doesn't help me solve the problem. Any suggestions ?

Routes.php

Route::post('user/follow/{id}', 'UserController@my_follow');

Profile.js

import React, { Component } from 'react';
import ReactDOM from 'react-dom';
import axios from 'axios';


export default class Example extends Component {

    constructor(props){
        super(props);

        this.state = {
            btnText: 'Follow',
            className: 'follow-button',
            user:{
                id:this.props.data
            }
        };
    }

      myfollow(user) {
        fetch('/user/follow/'+ this.state.user.id , { method: "POST" })
          .then(response => response.json())
          .then(response => {
            console.log(response);
          });
      };



    btnClick(){
        this.myfollow(this.state.user.id);

        if(this.state.btnText === 'Follow'){
          this.setState({
            btnText:'Following'
          })
        } else{
          this.setState({
            btnText: 'Follow'
          })
        }


        if(this.state.className === 'follow-button'){
          this.setState({
            className: 'following-button'
          })
        }
          else{
            this.setState({
              className: 'follow-button'
            })
          }

    }




    render(){
        return (
          <div className="followdoe">
            <button onClick={this.btnClick.bind(this)} className={this.state.className}>
              <p>{this.state.btnText}</p>
            </button>
          </div>
        );
    }

}

Profile.blade.php

  <div id="profile" data='{{ $user->name }}'></div>

This is what the back end looks like

Post Controller.php

public function my_follow(Request $request, $id)
{
    $user = auth()->user();

    if($user->id != $id && $otherUser = User::find($id)){

        $user->toggleFollow($otherUser);
    }

}
  • 写回答

1条回答 默认 最新

  • dongnang8192 2018-07-08 03:24
    关注

    I needed to change a couple things, after a bit of researching i found a solution.

    Profile.js

    import React, { Component } from 'react';
    import ReactDOM from 'react-dom';
    import axios from 'axios';
    
    
    export default class Example extends Component {
    
        constructor(props){
            super(props);
            let id = JSON.parse(this.props.data);
            this.state = {
                btnText: 'Follow',
                className: 'follow-button',
                user:{
                    id:id
                }
            };
        }
    
          myfollow(user) {
            axios('/user/follow/'+ this.state.user.id , { method: "POST" })
              .then(response => {
                console.log(response);
              });
          };
    
    
    
        btnClick(){
            this.myfollow();
    
            if(this.state.btnText === 'Follow'){
              this.setState({
                btnText:'Following'
              })
            } else{
              this.setState({
                btnText: 'Follow'
              })
            }
    
    
            if(this.state.className === 'follow-button'){
              this.setState({
                className: 'following-button'
              })
            }
              else{
                this.setState({
                  className: 'follow-button'
                })
              }
    
        }
    
    
    
    
        render(){
            return (
              <div className="followdoe">
                <button onClick={this.btnClick.bind(this)} className={this.state.className}>
                  <p>{this.state.btnText}</p>
                </button>
              </div>
            );
        }
    
    }
    if (document.getElementById('profile')) {
       var data = document.getElementById('profile').getAttribute('data');
       ReactDOM.render(<Example data={data} />, document.getElementById('profile'));
    }
    

    Profile.blade.php

    <div id="profile" data='{{ $user->id }}'></div>
    
    评论

报告相同问题?

悬赏问题

  • ¥15 素材场景中光线烘焙后灯光失效
  • ¥15 请教一下各位,为什么我这个没有实现模拟点击
  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 保护模式-系统加载-段寄存器