无法在Laravel中创建ajax请求

时间:2014-10-08 01:01:23

标签: php jquery ajax laravel

我正在研究Laravel,我正在创建一个Ajax注册表单。但它无法访问我的ajax进程中的URL。我有这样的错误:

XMLHttpRequest cannot load file:///C:/wampwwwlaravelblogpublic. Cross origin requests are only supported for HTTP. jquery-1.10.2.js:8706

这是我的示例代码:

控制器

public function index()
{   

    /*Login*/
    $title = Lang::get('login.panel_title');
    $username = Lang::get('login.username');
    $password = Lang::get('login.password');

    /*Registration*/
    $reg_username = Lang::get('register.username');
    $reg_lastname = Lang::get('register.lastname');
    $reg_firstname = Lang::get('register.firstname');
    $reg_gender = Lang::get('register.gender');
    $reg_email = Lang::get('register.email');
    $reg_birthday = Lang::get('register.birthday');

    $reg_month = Lang::get('register.months');
    $reg_password = Lang::get('register.password');
    $reg_conf_password = Lang::get('register.conf_password');

    $reg_path = public_path();

    return View::make('login', 
        array(
            'title'         => $title, 
            'username'      => $username,
            'password'      => $password,
            'reg_username'  => $reg_username,
            'reg_lastname'  => $reg_lastname,
            'reg_firstname' => $reg_firstname,
            'reg_gender'    => $reg_gender,
            'reg_email'     => $reg_email,
            'reg_birthday'  => $reg_birthday,
            'reg_month'     => $reg_month,
            'reg_password'  => $reg_password,
            'reg_conf_password' => $reg_conf_password,
            'reg_path'      => $reg_path
        )
    );

}

查看

<script type="text/javascript">

    $(document).ready(function() {

        $('#register-user').on('click', function(){

            console.log('clicked');

            var url_link = '{{ $reg_path }}';

            var $reg_username = $('input[name=reg_username]').val();
            var $reg_lastname = $('input[name=reg_lastname]').val();
            var $reg_firstname = $('input[name=reg_firstname]').val();
            var $reg_email = $('input[name=reg_email]').val();
            var $reg_gender = $('input[name=reg_gender]').val();
            var $reg_year = $('input[name=reg_year]').val();
            var $reg_month = $('input[name=reg_month]').val();
            var $reg_day = $('input[name=reg_day]').val();
            var $reg_password = $('input[name=reg_password]').val();
            //var $reg_conf_password = $('input[name=reg_conf_password]').val();

            /*
            if($reg_username == '') {
                $('.test').modal('show');
            }
            */

            $.ajax({
                url: url_link,
                data: {
                    username: $reg_username, 
                    lastname: $reg_lastname, 
                    firstname: $reg_firstname, 
                    email: $reg_email, 
                    gender: $reg_gender, 
                    year: $reg_year, 
                    month: $reg_month, 
                    day: $reg_day, 
                    password: $reg_password
                },
                type: 'post',
                dataType: 'json',
                beforeSend: function() {

                },
                success: function(data) {
                    console.log(data);
                },
                error: function() {

                }
            });

        });

    });

</script>

我在错误中注意到的是这部分C:/wampwwwlaravelblogpublic它删除了路径中的'/'斜杠。这是正常的吗?

或者有没有正确的方法来做简单的ajax过程?

这是所有人都感谢。

1 个答案:

答案 0 :(得分:1)

想要使用public_path()作为您的网址。那是你文件系统的路径!你只是想把它指向索引。因此,而不是{{ $reg_path }},请加入{{ URL::to('/') }}

干杯!

<script type="text/javascript">

$(document).ready(function() {

    $('#register-user').on('click', function(){

        console.log('clicked');

        var url_link = '{{ URL::to('/') }}';

        var $reg_username = $('input[name=reg_username]').val();
        var $reg_lastname = $('input[name=reg_lastname]').val();
        var $reg_firstname = $('input[name=reg_firstname]').val();
        var $reg_email = $('input[name=reg_email]').val();
        var $reg_gender = $('input[name=reg_gender]').val();
        var $reg_year = $('input[name=reg_year]').val();
        var $reg_month = $('input[name=reg_month]').val();
        var $reg_day = $('input[name=reg_day]').val();
        var $reg_password = $('input[name=reg_password]').val();
        //var $reg_conf_password = $('input[name=reg_conf_password]').val();

        /*
        if($reg_username == '') {
            $('.test').modal('show');
        }
        */

        $.ajax({
            url: url_link,
            data: {
                username: $reg_username, 
                lastname: $reg_lastname, 
                firstname: $reg_firstname, 
                email: $reg_email, 
                gender: $reg_gender, 
                year: $reg_year, 
                month: $reg_month, 
                day: $reg_day, 
                password: $reg_password
            },
            type: 'post',
            dataType: 'json',
            beforeSend: function() {

            },
            success: function(data) {
                console.log(data);
            },
            error: function() {

            }
        });

    });

});