首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Ajax调用在成功时返回CSRF-token,但不返回消息

Ajax调用在成功时返回CSRF-token,但不返回消息
EN

Stack Overflow用户
提问于 2016-01-19 19:24:18
回答 2查看 737关注 0票数 0

当从PHP调用ajax时,这个问题工作得很好,但是我似乎不能让它与Laravel 5.2一起工作。我想做的就是在提交表单时使用AJAX发送电子邮件。

这是我用于发送电子邮件的routes.php条目:

代码语言:javascript
复制
Route::post('/send', 'CommsController@send');

这是我的layout.blade.php文件:

代码语言:javascript
复制
<head>
    <meta name="csrf-token" content="{{ csrf_token() }}" />
</head>

$( document ).ready(function() {
    $.ajaxSetup({
        headers: {
            'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')
        }
    });

    ...

    $( "#send" ).button().click(function(event) {
        var form       = $( "#contact-form" ),
            postAction = form.attr('action'),
            isValid    = form.valid(),
            valSum     = $(".validation-summary");

        if ( isValid ) {
            var postData = {
                message_type: "contact"
                ,first_name: first_name.val()
                ,last_name: last_name.val()
                ,email: email.val()
                ,message: message.val()
            };

            // process the form
            $.ajax({
                type: "POST",
                // our data object
                url: postAction,        // the url where we want to POST
                data: postData
            })
            // using the done promise callback
            .done(function(data) {
                // log data to the console so we can see
                console.log(data); 
                // here we will handle errors and validation messages
                if ( ! data.success ) {
                    valSum.removeClass( "success" );
                    valSum.addClass( "validation-summary error" );
                    valSum.html( data );
                } else {
                    // $( ".validation-summary" ).html( "Message sent." );
                    valSum.html( data.message );
                }
            })
            // using the fail promise callback
            .fail(function(data) {
                // show any errors
                // best to remove for production
                console.log(data);

                valSum.removeClass( "success" );
                valSum.addClass( "validation-summary error" );
                valSum.html( "Server Error: " + data.statusText + " processing your request, please contact Dorothea or report a bug." );
            });
            // stop the form from submitting the normal way and refreshing the page
            event.preventDefault();
        } else {
            return false;
        }
    });

最后是我的CommsController send()函数:

代码语言:javascript
复制
public function send(Request $request) {
    //check if its our form
    if ( Session::token() !== Request::get( 'csrf-token' ) ) {
        return Response::json( array(
            'message' => 'Unauthorized attempt to create setting'
        ));
    }

    $message_type = strval(Request::input('message_type'));
    $first_name = strval(Request::input('first_name'));
    $last_name = strval(Request::input('last_name'));
    $email = strval(Request::input('email'));
    $message = strval(Request::input('message'));

    $to  = "robinson.jeanine6@gmail.com";

    // subject
    $subject = "Dorothea - Contact Us";

    Mail::send('email.contact', ['request' => Request::all()], function ($message) use ($request) {
        $message->from($this->email, $this->email);
        $message->to($user->email, $user->email)->subject($subject);
    });

    $response = array(
        'status' => 'success',
        'message' => 'Message sent.',
    );

    return Response::json( $response );
}

这个问题与我发布的here相关,但现在从AJAX调用返回的不是MethodNotAllowedHttpException,而是CSRF-token值。

EN

回答 2

Stack Overflow用户

发布于 2016-01-19 21:55:19

.success替换.done回调,即可获取返回数据。

代码语言:javascript
复制
.success(function(data) {
    if (data.status != 'success' ) {
        valSum.removeClass( "success" );
        valSum.addClass( "validation-summary error" );
    }

    // eventually, print out the return message
    valSum.html(data.message);
})
票数 0
EN

Stack Overflow用户

发布于 2016-02-01 22:12:34

我只是想让每个试图帮助我的人知道,如果我将post请求更改为get请求,那么在Laravel中发送电子邮件就可以正常工作。

我不太确定为什么会这样,所以如果有人能解释一下为什么会是这样,那就太好了。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/34875516

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档