无法通过javascript函数调用路由

无法通过javascript函数调用路由,javascript,laravel,routes,Javascript,Laravel,Routes,我试图通过javascript函数调用laravel路由。我已经在互联网上和这里探索了所有的方法,但我没有找到我的解决方案。我提到了一个关于如何在javascript函数中调用route的关于堆栈溢出的答案,但它对我不起作用。我的路线: Route::get(“编辑联系人/{id}”)ContactController@edit'); Javascript函数: { var url = '{{ route("edit_contact",":id") }}';

我试图通过javascript函数调用laravel路由。我已经在互联网上和这里探索了所有的方法,但我没有找到我的解决方案。我提到了一个关于如何在javascript函数中调用route的关于堆栈溢出的答案,但它对我不起作用。我的路线:

Route::get(“编辑联系人/{id}”)ContactController@edit');

Javascript函数:

    {
         var url = '{{ route("edit_contact",":id") }}';
         url = url.replace(':id', count);
         document.location.href = url;          
    }
count是在javascript中定义的,我需要在路由中传递它。 问题是它不断地给出错误。。
未定义路由[编辑联系人]。


当浏览html时,所有这些路由都可以正常工作。我也试着通过这个调用其他路线,但没有一条是有效的

按以下方式命名您的路线:

Route::get("edit_contact/{id}",'ContactController@edit')->name('edit_contact');
试一试

let count=3

document.location.href=`edit_contact/${count}`试试这件。它应该在以下方面发挥作用:

Route::get("edit_contact/{id}",'ContactController@edit');

javascript code

<script type="text/javascript">

$("#valueid").change(function(e){

       var id= e.target.value; // the id to be pass
        //alert(id);
        //checking the state of the domain if secured or not
           if ("https:" == document.location.protocol) {
            //alert('secured');
            location.href="https://www.example.com/edit_contact/"+id;
        } else {
            //alert('not secured');
             location.href="http://www.example.com/edit_contact/"+id;
        }

    });

</script>
Route::get(“编辑联系人/{id}”)ContactController@edit');
javascript代码
$(“#valueid”).change(函数(e){
var id=e.target.value;//要传递的id
//警报(id);
//检查域的状态(是否安全)
if(“https:==document.location.protocol){
//警报(“安全”);
location.href=”https://www.example.com/edit_contact/“+id;
}否则{
//警报(“未安全”);
location.href=”http://www.example.com/edit_contact/“+id;
}
});
在路由文件中:(web.php)

在js代码中:

这里我使用了计数值3作为示例。您可以使用count的实际值

<script>
   var count = 3;

   var baseurl = window.location.protocol + "//" + window.location.host;

   var url = baseurl + '/edit_contact/' + count;            

   document.location.href = url; 

</script>

var计数=3;
var baseurl=window.location.protocol+“/”+window.location.host;
var url=baseurl+'/edit_contact/'+count;
document.location.href=url;

O谢谢。。我从来没有想过这个问题会如此微不足道。如果我的答案有帮助,请将其标记为正确答案。如果我们必须通过javascript传递一个或两个变量,那会是什么情况?我曾尝试通过“:b_id”添加它,但它不起作用。虽然这段代码可能会解决这个问题,但它如何以及为什么解决这个问题会真正有助于提高您的帖子质量,并可能导致更多的投票。请记住,你是在将来回答读者的问题,而不仅仅是现在提问的人。请在回答中添加解释,并说明适用的限制和假设。
Route::get("edit_contact/{id}",'ContactController@edit');
<script>
   var count = 3;

   var baseurl = window.location.protocol + "//" + window.location.host;

   var url = baseurl + '/edit_contact/' + count;            

   document.location.href = url; 

</script>