如何使用ajax序列化调用控制器的路由

时间:2016-04-15 13:09:04

标签: jquery ajax laravel

我正在laravel中制作一个ajax序列化,但我已经咨询并注意到所有这种类型的ajax从:: open的形式调用路由,没有办法调用固定路由?像这样的东西:

var formId = '#radicado';

var token = document.getElementById('token').value;
$.ajax({
    async: true,
    headers: {'X-CSRF-TOKEN': token},
    url: ip+'/storeVersion',
    type:  'POST',
    data: $(formId).serialize(),
    dataType: 'html',
    success: function(result){
        $(formId)[0].reset();
        alert(result);
        document.getElementById("version").style.display = "none";
        document.getElementById("preview").style.display = "none";
        parent.formulario.location.reload() 
    },
    error: function(){
        alert('No se ha actualizado el documento.');
    }
});

和路线

Route::post('storeVersion','RadicadoController@storeVersion');

2 个答案:

答案 0 :(得分:0)

You can use the url() helper method to generate a fully qualified url: https://laravel.com/docs/5.1/helpers#method-url

...
$.ajax({
    async: true,
    headers: {'X-CSRF-TOKEN': token},
    url: "{{ url('storeVersion') }}", // output http://yourApp.com/storeVersion
...

答案 1 :(得分:0)

I wouldn't recommend you to mix javascript with blade code, it's not a good practice. I'll set the ip variable with the base url and then construct the url from there.

I based my answer in comments and other answers as I don't understand the question very well.