jQuery Ajax将发布数据发送到Laravel控制器类中的方法

时间:2018-01-12 18:28:22

标签: php jquery html ajax laravel

我正在尝试将数据发送到Laravel Project中的Controller方法,请求是我在项目的Route文件中指定的POST,并且url结构为 http://www.globfinances.com/mail-walker

Controller类有一个方法,它将处理发布数据并使用它发送电子邮件,并使用jquery $ .post ajax函数发送post请求,以发送我从数据获取的数据 - **按钮的属性触发事件。

这是laravel Route文件中的我的代码:

/* Ninja Management Route List */
Route::post('mail-walker',['as'=>'mail-walker','uses'=>'UserController@mailWizard']);

以下是处理请求的方法(在UserController类中)

/******** CONSTRUCTING THE MAIL RUNNER WIZARD FUNCTION ************/
public function mailWizard(Request $request){
    $thisUser = User::findOrFail($request->id);
    // HERE LETS CHECK REQUEST PARAMETERS AND KNOW WHAT TO PASS TO MAIL
    if($request->type == "dpo" || $request->type == "wun"){
        // Mail data
        $val = [
          'amount' => $request->amount,
            'currency_sign' => $request->c_sign,
            'payment_type' => $request->p_type,
            'type' => $request->type,
            'method' => $request->method,
            'message' => $request->message,
            'userid' => $id,
        ];
        // Now lets send email
        $this->mailRunnerAdmin($val, $thisUser);
        $this->mailRunnerUser($val, $thisUser);
    }
    else{
        // Mail data
        $val = [
          'amount' => $request->amount,
            'currency_sign' => $request->c_sign,
            'payment_type' => $request->p_type,
            'type' => $request->type,
            'userid' => $id,
        ];
        // Now lets send email
        $this->mailRunnerAdmin($val, $thisUser);
        $this->mailRunnerUser($val, $thisUser);
    }
}

/********** CONSTRUCTING THE MAIL RUNNER FUNCTION *************/
public function mailRunnerAdmin($mailData, $thisUser)
{
    $general = GeneralSetting::first();

        $mailData['site_title'] = $general->title;
        $mailData['site_footer'] = $general->$general->footer_bottom_text;
        $mailData['email'] = $thisUser['email'];
        $mailData['name'] = $thisUser['name'];
        $mailData['g_email'] = $general->email;
        $mailData['g_title'] = $general->title;
        $mailData['subject'] = 'Request for payment from '.ucfirst($thisUser['name']);

        Config::set('mail.driver','mail');
        Config::set('mail.from',$general->email);
        Config::set('mail.name',$general->title);
        Mail::send('auth.mailRunnerAdmin', $mailData, function ($m) use ($mailData) {
            $m->from($mailData['g_email'], $mailData['g_title']);
            $m->to($mailData['g_email'], $mailData['g_title'])->subject($mailData['subject']);
        });

}


public function mailRunnerUser($mailData, $thisUser)
{
    $general = GeneralSetting::first();

        $mailData['site_title'] = $general->title;
        $mailData['site_footer'] = $general->$general->footer_bottom_text;
        $mailData['email'] = $thisUser['email'];
        $mailData['name'] = $thisUser['name'];
        $mailData['g_email'] = $general->email;
        $mailData['g_title'] = $general->title;
        $mailData['subject'] = 'Payment Request Confirmation';

        Config::set('mail.driver','mail');
        Config::set('mail.from',$general->email);
        Config::set('mail.name',$general->title);
        Mail::send('auth.mailRunnerUser', $mailData, function ($m) use ($mailData) {
            $m->from($mailData['g_email'], $mailData['g_title']);
            $m->to($mailData['email'], $mailData['name'])->subject($mailData['subject']);
        });

}

以下是制作Ajax请求的JQuery代码

$(".mailManTrigger").click(function(e){
e.preventDefault();
// Now lets delay as we call ajax function
// Setting the trigger variables
// We begin processing
var id = $(this).attr("data-id");
var amt = $(this).attr("data-amount");
var currency_sign = $(this).attr("data-c_sign");
var payment_type = $(this).attr("data-p_type");
var type = $(this).attr("data-type");
var url = $(this).attr("data-url");
var token = $(this).attr("data-token");
var method = $(this).attr("data-method");
var message = $(this).closest('form').find('textarea[name="message"]').val();

// Lets call our mail man to deliver message
// Lets check type to know how to send
    if(type!="dpo" || type!="wun"){
        $.post(
                    url,
                    {
                       _token: token,
                       amount : amt,
                       c_sign : currency_sign,
                       p_type : payment_type,
                       type : type,
                       id : id
                     },
                    function(data) {
                       // Now lets return
                       $(this).closest('form').submit();
                    }
           );
    }
    else{
        $.post(
                    url,
                    {
                       _token: token,
                       amount : amt,
                       c_sign : currency_sign,
                       p_type : payment_type,
                       type : type,
                       id : id,
                       method : method,
                       message : message
                     },
                    function(data) {
                       // Now lets return
                        $(this).closest('form').submit();
                    }
           );
    }
  });

这是触发事件的表单按钮(查看数据 - 属性)**

  <button class="btn btn-danger btn-block btn-icon icon-left mailManTrigger" data-id="7" data-amount="0.524" data-c_sign="NGN" data-p_type="Credit Card Payment" data-type="ccd" data-url="http://www.globfinances.com/mail-walker" data-token="VYceSdYbhocs7GTOBcws2sg4cysqRT8MVJuNGV2q"><i class="fa fa-send"></i>Add Fund Now</button>

这是Jquery代码应该在ajax成功时提交的形式

  <form method="POST" action="http://www.globfinances.com/stripe-preview" accept-charset="UTF-8"><input name="_token" type="hidden" value="VYceSdYbhocs7GTOBcws2sg4cysqRT8MVJuNGV2q">
                                        <input type="hidden" name="amount" value="0.52">
                                        <input type="hidden" name="fund_id" value="16">
                                        <input type="hidden" name="transaction" value="180112q7mok811">
                                        <input type="hidden" name="charge" value="12">
                                            <div class="form-group">
                                                <div class="col-sm-6 col-sm-offset-3">
                                                    <button class="btn btn-danger btn-block btn-icon icon-left mailManTrigger" data-id="7" data-amount="0.524" data-c_sign="NGN" data-p_type="Credit Card Payment" data-type="ccd" data-url="http://www.globfinances.com/mail-walker" data-token="VYceSdYbhocs7GTOBcws2sg4cysqRT8MVJuNGV2q"><i
                                                                class="fa fa-send"></i>Add Fund Now</button>
                                                </div>
                                            </div>
                                        </form>

问题

问题是,当我点击按钮没有任何反应时,我没有收到电子邮件消息我打算发送代码,表单未提交,这意味着ajax调用不成功。 我不知道出了什么问题,我需要帮助解决这个问题。

我想要实现的目标

我想在点击按钮时向用户和管理员发送电子邮件,然后在jQuery函数的开头提交我阻止提交的表单。

任何助理都将受到高度赞赏!

0 个答案:

没有答案