문제

I am working with Laravel 4 and I want to perform validation with Ajax. I have 2 main problems: 1. The URL at Ajax is static, which means that if I have my app online I should put the URL for online and locally doesn't works 2. my route is insur_docs/{id} how should be URL for this?

jQuery('form#insur_docs_update').submit(function()
    {
        jQuery.ajax({
            url: "http://localhost:8080/insur_docs/{id}", //my url I don't know how to put it
            type: "post",
            data: jQuery('form#insur_docs_update').serialize(),
            datatype: "json",
            beforeSend: function()
            {
                jQuery('#ajax-loading').show();
                jQuery(".glyphicon-warning-sign").hide();
            }
        })
                .done(function(data)
                {
                    $('#validation-div').empty()
                    if (data.validation_failed === 1)
                    {
                        var arr = data.errors;
                        jQuery.each(arr, function(index, value)
                        {
                            if (value.length !== 0)
                            {
                                $("#validation-div").addClass('alert alert-danger');
                                document.getElementById("validation-div").innerHTML += '<span class="glyphicon glyphicon-warning-sign"></span>' + value + '<br/>';
                            }
                        });
                        jQuery('#ajax-loading').hide();
                    }
                })
                .fail(function(jqXHR, ajaxOptions, thrownError)
                {
                    alert('No response from server');
                });
        return false;
    });

routes.php

Route::get('insur_docs/{id}', 'Insur_DocController@edit');

controller

public function update($id) {
        Input::flash();
        $data = [
            "errors" => null
        ];
        $rules = array(
            "ownership_cert" => "required",
            "authoriz" => "required",
            "drive_permis" => "required",
            "sgs" => "required",
            "tpl" => "required",
            "kasko" => "required",
            "inter_permis" => "required",
        );
        $validation = Validator::make(Input::all(), $rules);
        if ($validation->passes()) {
            $car_id = DB::select('select car_id from insur_docs where id = ?', array($id));
            $data = InsurDoc::find($id);
            $data->ownership_cert = Input::get('ownership_cert');
            $data->authoriz = Input::get('authoriz');
            $data->drive_permis = Input::get('drive_permis');
            $data->sgs = Input::get('sgs');
            $data->tpl = Input::get('tpl');
            $data->kasko = Input::get('kasko');
            $data->inter_permis = Input::get('inter_permis');
            $data->save();
            return Redirect::to('car/' . $car_id[0]->car_id);
        } else {
            if (Request::ajax()) {
                $response_values = array(
                    'validation_failed' => 1,
                    'errors' => $validation->errors()->toArray()
                        );
                return Response::json($response_values);
            }             
        }
    }
도움이 되었습니까?

해결책

Use laravel's url generator helper to create your form's action:

<form action="{{ URL::action('Insur_DocController@edit', $id) }}" method="post">

You can access it in your javascript:

jQuery('form#insur_docs_update').submit(function()
{
    var url = $(this).attr("action");

    jQuery.ajax({
        url: url,
        type: "post",
        data: jQuery('form#insur_docs_update').serialize(),
        datatype: "json",
        beforeSend: function()
        {
            jQuery('#ajax-loading').show();
            jQuery(".glyphicon-warning-sign").hide();
        }
    });
}

EDIT

You're second problem is that you're redirecting in response to the ajax call, and that does not redirect the page. You'll need to return the url and do the redirect in javascript like this.

Controller:

return Response::json(["redirect_to" => 'car/' . $car_id[0]->car_id]);

JS (just the relevant part):

.done(function(data)
{
    $('#validation-div').empty()
    if (data.validation_failed === 1)
    {
        // your code
    } else {
        window.location = data.redirect_to;
    }
})

다른 팁

var myUrlExtension = "whatever.php"

and inside the ajax

url: "http://localhost:8080/insur_docs/" + myUrlExtension
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top