Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/68.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何将javascript变量放入url()函数中?_Javascript_Jquery_Laravel_Laravel 5.7 - Fatal编程技术网

如何将javascript变量放入url()函数中?

如何将javascript变量放入url()函数中?,javascript,jquery,laravel,laravel-5.7,Javascript,Jquery,Laravel,Laravel 5.7,我已经在我的代码上绞尽脑汁几个小时了,但是没有找到将javascript变量放入LaravelURL中的正确方法。每当我将变量作为url中的参数放置时,它都会被视为字符串。下面是我的代码。请帮忙 javascript文件 这在{{url'+txt_url+'}中不起作用。我一直在扭曲报价的位置,但一点运气都没有。其结果是http://localhost:8000/txt_url. 我希望是这样http://localhost:8000/addamortbusinessloan 或http://l

我已经在我的代码上绞尽脑汁几个小时了,但是没有找到将javascript变量放入LaravelURL中的正确方法。每当我将变量作为url中的参数放置时,它都会被视为字符串。下面是我的代码。请帮忙

javascript文件


这在{{url'+txt_url+'}中不起作用。我一直在扭曲报价的位置,但一点运气都没有。其结果是http://localhost:8000/txt_url. 我希望是这样http://localhost:8000/addamortbusinessloan 或http://localhost:8000/addamortization不需要用逗号,因为没有逗号也可以。检查这个

function generateSchedule( id, loan_type ){

          var txt_url;           

          if( loan_type == "Business" ){
            txt_url = "addamortbusinessloan"; 
          }else{
            txt_url = "addamortization";
          }

        var base_url="http://localhost:8000/"

        $.post( base_url+txt_url, {'loanId':id}, function( data ) {

         }, "json");

    }

使用此blade指令将变量导出到JS,方法很简单:spatie/laravel blade javascript

实际上您不能,但如果需要,您可以像这样在app.blade文件中定义全局变量

var BASE_URL='{{url('')}}'
</script>
或者,您可以在head中的meta标记中定义它,如下所示: 在javascript文件中,您可以通过jquery获得它 $'meta[name=base_url]'.attr'content'

将javascript变量放入laravel url中。你不能这样。我想您上面的代码是一个php模板。但是generateSchedule在浏览器中被调用。因此,JavaScript中根本不存在txt_url变量,这就是模板引擎呈现您看到的静态值的原因。
function generateSchedule( id, loan_type ){

          var txt_url;           

          if( loan_type == "Business" ){
            txt_url = "addamortbusinessloan"; 
          }else{
            txt_url = "addamortization";
          }

        var base_url="http://localhost:8000/"

        $.post( base_url+txt_url, {'loanId':id}, function( data ) {

         }, "json");

    }
var BASE_URL='{{url('')}}'
</script>