简体   繁体   English

Laravel 5使用Ajax发送密码重置链接

[英]Laravel 5 Send Password Reset Link using Ajax

I have this code: 我有这个代码:

jQuery.ajax({
    type:"POST",
    url:"/password/email/",
    data:{
        _token: jQuery("#forgotPasswordContainer input[name='_token']").val(),
        email: email
    },
    dataType:'json',
    beforeSend:function(){

    },
    success:function(data){

    },
    complete:function(){

    }
});

it seems that it is doing nothing. 似乎它什么都不做。

When I checked firebug, i am getting an html page containing the html of /password/email page. 当我检查firebug时,我得到一个包含/password/email页面html的html页面。

I am guessing I need to modify how sending password reset link works. 我猜我需要修改发送密码重置链接的工作原理。

Can someone help me on this matter. 有人可以帮我解决这个问题。

Your help will be greatly appreciated! 对你的帮助表示感谢!

Thanks! 谢谢!

Ok, I managed to solved this one by putting this on my PasswordController.php 好吧,我设法解决了这个问题,把它放在我的PasswordController.php上

public function getEmail(Request $request)
{
    $this->validate($request, ['email' => 'required|email']);

    $response = $this->passwords->sendResetLink($request->only('email'), function($m)
    {
        $m->subject($this->getEmailSubject());
    });

    switch ($response)
    {
        case PasswordBroker::RESET_LINK_SENT:
            return[
                'error'=>'false',
                'msg'=>'A password link has been sent to your email address'
            ];

        case PasswordBroker::INVALID_USER:
            return[
                'error'=>'true',
                'msg'=>"We can't find a user with that email address"
            ];
    }
}

I am not sure if this is efficient but this works for me. 我不确定这是否有效但这对我有用。 Hope this helps someone. 希望这有助于某人。

Thanks! 谢谢!

Laravel 5.2 Laravel 5.2

If you want to customized or changed the POST URL that you send via AJAX here is the complete answer: 如果您想自定义或更改通过AJAX发送的POST URL,请完整答案:

ajax.js: ajax.js:

jQuery.ajax({
    type:"POST",
    url:"/user/password/reset",
    data:{
        _token: jQuery("#forgotPasswordContainer input[name='_token']").val(),
        email: email
    },
    dataType:'json',
    beforeSend:function(){

    },
    success:function(data){

    },
    complete:function(){

    }
});

routes.php: routes.php文件:

Route::post('user/password/reset', [
      'uses'         => 'Controller_name@index'
]);

App/Http/Controllers/Controller_name.php: 应用程序/ HTTP /控制器/ Controller_name.php:

<?php
namespace App\Http\Controllers;

use Illuminate\Http\Request;

use App\Http\Requests;

use Illuminate\Contracts\Auth\PasswordBroker;

class Controller_name extends Controller
{
    public function index(Request $request, PasswordBroker $passwords)
    {
        if( $request->ajax() )
        {
            $this->validate($request, ['email' => 'required|email']);

            $response = $passwords->sendResetLink($request->only('email'));

            switch ($response)
            {
                case PasswordBroker::RESET_LINK_SENT:
                   return[
                       'error'=>'false',
                       'msg'=>'A password link has been sent to your email address'
                   ];

                case PasswordBroker::INVALID_USER:
                   return[
                       'error'=>'true',
                       'msg'=>"We can't find a user with that email address"
                   ];
            }
        }
        return false;
    }
}

resources/views/ 资源/视图/

Create a new directory auth > emails > password.blade.php for email template: 为电子邮件模板创建新目录auth > emails > password.blade.php

Click here to reset your password .<br />
<a target="_blank" href="{{ url('password/reset', $token).'?email='.urlencode($user->getEmailForPasswordReset()) }}">Click to Reset Password</a>

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

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