在路由中使用js函数变量 [英] Use js function variable in route

查看:148
本文介绍了在路由中使用js函数变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想通过ajax将ID传递到我的路线,但不能这样做:

I want to pass ID to my route via ajax but cant do that:

function test(id){
   $('#items').DataTable({
      ajax: {
      url: '{!!  route('routename', ['menu_id' => id]) !!}',
          type: 'POST'
      },

表示使用未定义的常量ID. 如何使用JavaScript变量来传递路线?

It says Use of undefined constant id. How can i use javascript variables to pass through route?

推荐答案

由于在客户端代码之前执行了服务器端代码,因此无法在PHP中使用javascript变量.因此,您可以将JS变量分配给相等的PHP变量,但不能相反.检查一下: https://stackoverflow.com/a/2379251/7377984

It is not possible to use javascript variables in PHP as server side code is executed before client side code. So you may assign JS variables to equal PHP variables but not the other way around. Check this out: https://stackoverflow.com/a/2379251/7377984

您有以下选择:

  1. 代替路由参数,您可以使用
  1. Instead of route parameters, you may use request parameters
  2. If you really want to use route parameters, you would have to store your routes in your JS code (e.g. if the route is http://www.example.com/routename/{menu_id}, you may store the route variable as an absolute url like so: var route = http://www.example.com/routename/ or as a relative url like so: var route = /routename/ and then in the ajax request append the route parameter like so url: route + id

这篇关于在路由中使用js函数变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆