2016-04-15 46 views
0

我在laravel中做了一个ajax序列化,但是我已经咨询过并且注意到所有这种类型的ajax都调用了来自form :: open的路由,没有办法调用固定路线?是这样的:如何用ajax序列化调用控制器的路由

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','[email protected]'); 
+1

为什么不能简单地插入* fixed * URL('url:'http:// example.com/path/to/method''),然后将该URL路由到您的控制器并处理来自那里的数据? – Marcus

+0

此javascript是否被加载到刀片文件中?如果'/ storeVersion'出现在root之后,即'yourapp.com/storeVersion',则应该使用相对路径并将url设置为'/ storeVersion'。这样,它可以在任何域上运行。 – user3158900

回答

0

可以使用url()辅助方法来生成一个完全合格的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 
... 
0

我劝你不要来将JavaScript与刀片代码混合使用,这不是一个好习惯。我将使用基础网址设置ip变量,然后从那里构建网址。

我根据我的回答在评论和其他答案,因为我不明白这个问题很好。