doubaoguo7469 2014-05-10 09:33
浏览 147
已采纳

如何动态设置Ajax URL?

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);
            }             
        }
    }
  • 写回答

2条回答 默认 最新

  • douyong1908 2014-05-10 10:00
    关注

    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;
        }
    })
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 如何在scanpy上做差异基因和通路富集?
  • ¥20 关于#硬件工程#的问题,请各位专家解答!
  • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
  • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
  • ¥30 截图中的mathematics程序转换成matlab
  • ¥15 动力学代码报错,维度不匹配
  • ¥15 Power query添加列问题
  • ¥50 Kubernetes&Fission&Eleasticsearch
  • ¥15 報錯:Person is not mapped,如何解決?
  • ¥15 c++头文件不能识别CDialog