【问题标题】:Laravel:How to send Ajax to controller without formLaravel:如何在没有表单的情况下将 Ajax 发送到控制器
【发布时间】:2015-01-03 18:56:41
【问题描述】:

我在我的网站中使用了一个 jquery 插件。我想先验证一个表单,然后将另一个数据发送到控制器,但不通过表单。首先验证是已经返回成功反馈。但是当第二个被发送时,没有任何反馈返回。这是我的代码

route.php

Route::controller('designs', 'DesignsController');

DesignsController.php

public function postTest() {
    $validator = Validator::make(Input::all(), Design::$rules);
    if ($validator->passes()) {

        $response = array(
            'status' => 'success'
        );
        return Response::json( $response );

    }
    $response = array(
        'status' => 'fail'
    );
    return Response::json( $response );
}
public function postSaveimage() {

    //Save Image complete return $success=1

    if($success)
     $response = array(
        'status' => 'save image success'
    );
    else
     $response = array(
        'status' => 'Fail'
    );
    return Response::json( $response );

}

jquery

$('#design_form').on( 'submit' ,function() {

             //Validate First form data
            $.post(
                $( this ).prop( 'action' ),
                {
                    "_token": $( this ).find( 'input[name=_token]' ).val(),
                    "category_id": $( '#form_category_id' ).val(),
                    "title": $( '#form_title' ).val(),
                    "user_id": $( '#form_user_id' ).val(),
                },
                function( data ) {
                    //if validate fail
                    if(data.status=='fail')
                    {
                        alert('data.status');
                    }
                    //if validate pass
                    else
                    {
                       //Sent second data
                        $.post("designs/saveimage", 
                        { 
                            "_token": $( this ).find( 'input[name=_token]' ).val(),
                            'base64_image': yourDesigner.getProductDataURL() 
                        }, function(data) {
                            if(data) {
                                alert(JSON.stringify(data));
                            }
                            else {

                                alert('fail!');
                            }
                        });
                    }
                },
                'json'
            );
            return false;
        });

【问题讨论】:

  • 嗨,什么是 var_export( $_POST );在你的控制器代码中说。
  • 尝试使用 Firebug 来查看 Ajax 响应。然后你就会知道第二次 POST 得到的实际回报是什么
  • 它说 500 Internal Server Error "Illuminate\Session\TokenMismatchException" 猜我要创建新令牌对吗?

标签: php jquery ajax laravel laravel-4


【解决方案1】:
$('#design_form').on( 'submit' ,function() {

        var _this = this;
         //Validate First form data
        $.post(
            $( this ).prop( 'action' ),
            {
                "_token": $( this ).find( 'input[name=_token]' ).val(),
                "category_id": $( '#form_category_id' ).val(),
                "title": $( '#form_title' ).val(),
                "user_id": $( '#form_user_id' ).val(),
            },
            function( data ) {
                //if validate fail
                if(data.status=='fail')
                {
                    alert('data.status');
                }
                //if validate pass
                else
                {
                   //Sent second data
                    $.post("designs/saveimage", 
                    { 
                        "_token": $( _this ).find( 'input[name=_token]' ).val(),
                        'base64_image': yourDesigner.getProductDataURL() 
                    }, function(data) {
                        if(data) {
                            alert(JSON.stringify(data));
                        }
                        else {

                            alert('fail!');
                        }
                    });
                }
            },
            'json'
        );
        return false;
    });

添加新行:

var _this = this;

在第二次发送中:

"_token": $( _this ).find( 'input[name=_token]' ).val()

【讨论】:

  • 谢谢!!像魅力一样工作!感谢您指出我的错误。我现在明白了。
【解决方案2】:

在您的第二个 ajax 帖子中,您必须指明主机

如果是在js文件中,则必须指定主机

  $.post("http://www.host.com/public/designs/saveimage")

如果在视图中

  $.post("{{URL::to('designs/saveimage')}}");

您应该检查帖子的去向以及路线是否正确 在 Firefox 中使用 firebug 或在 google chrome 中使用控制台。 .

【讨论】:

  • Firebug 说 TokenMismatchException。我认为这是关于我如何使用令牌传递数据
猜你喜欢
  • 2016-12-14
  • 1970-01-01
  • 2015-09-19
  • 1970-01-01
  • 2015-12-17
  • 1970-01-01
  • 2020-02-21
  • 2020-10-19
  • 1970-01-01
相关资源
最近更新 更多