【问题标题】:Axios post is not submitting in laravelAxios 帖子未在 laravel 中提交
【发布时间】:2021-01-25 03:53:24
【问题描述】:

我正在向 laravel 登录控制器发送 axios 请求。 axios代码

         attemptLogin()
            {
              axios.post('/login',

              {email:this.email, password:this.password, remember_me:this.remember_me},
               { headers: {
                  'Content-type': 'application/x-www-form-urlencoded',
                          },
               }).then((resp) => {
                    console.log(resp);
                    // location.reload();
                }).catch(error => {
                    this.errorMessage = error.message;
                     console.error("There was an error!", error);
                })
            }

登录控制器代码

protected function authenticated(Request $request, $user)
    {
        // return "Login successfull";
        return view('home');
    }

我不知道发生了什么,显示控制台没有错误。

【问题讨论】:

  • 您是否检查过您的网络标签是否有任何活动?
  • 是的,没有活动
  • 您是否在调用函数 attemptLogin 并且您在控制台或网络选项卡中得到了什么?
  • 你在 Laravel 中使用 web 路由吗?因为如果你是,因为不正确的信用 laravel 将你返回到上一页,所以可能是你在响应中被重定向。也尝试包括 csrf 令牌。
  • 您能否提供一些有关如何调用此代码的详细信息?

标签: ajax laravel axios http-post


【解决方案1】:

浏览器是否显示任何内容?根据您所指出的,您应该确保请求正在访问 LoginController 中的登录功能。您必须点击该特征类,然后处理您的请求。

class LoginController extends Controller
{
   use AuthenticatesUsers;

   public function __construct()
   {
      $this->middleware('guest')->except('logout');
   }
}

【讨论】:

    【解决方案2】:

    实际上是我自己回答这个问题,因为我找到了确切的问题, 我在 axios 语法上做错了,

    正确的语法如下

    attemptLogin()
    {
        axios.post('/login', {
        email:this.email, password:this.password, remember_me:this.remember_me
       }).then(function (response) {
          console.log(response);
       }).catch(function (error) {
          console.log(error);
       });
    }
    

    【讨论】:

      猜你喜欢
      • 2020-09-26
      • 2020-02-19
      • 2017-09-13
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-01-16
      • 2019-01-26
      • 1970-01-01
      相关资源
      最近更新 更多