【问题标题】:Laravel 5 Send Password Reset Link using AjaxLaravel 5 使用 Ajax 发送密码重置链接
【发布时间】:2015-07-18 21:13:29
【问题描述】:

我有这个代码:

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(){

    }
});

它似乎什么也没做。

当我检查 firebug 时,我得到一个包含 /password/email 页面的 html 的 html 页面。

我猜我需要修改发送密码重置链接的工作方式。

有人可以帮我解决这个问题吗?

您的帮助将不胜感激!

谢谢!

【问题讨论】:

  • 你的 php 脚本是什么
  • @HarigovindR 这是您重置密码时 Laravel 的默认链接。因此,当您检查文件 PasswordController.php 时,PHP 代码是相同的

标签: php jquery ajax laravel laravel-5


【解决方案1】:

好的,我设法通过将它放在我的 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"
            ];
    }
}

我不确定这是否有效,但这对我有用。希望这对某人有所帮助。

谢谢!

【讨论】:

    【解决方案2】:

    Laravel 5.2

    如果您想自定义或更改您通过AJAX 发送的POST URL,这里是完整的答案:

    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:

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

    App/Http/Controllers/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;
        }
    }
    

    资源/视图/

    为电子邮件模板创建一个新目录auth &gt; emails &gt; 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>
    

    【讨论】:

      猜你喜欢
      • 2019-01-12
      • 2017-10-17
      • 1970-01-01
      • 1970-01-01
      • 2017-10-21
      • 2017-08-02
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多