简体   繁体   English

从Javascript | jQuery和Symfony2项目中的URL获得价值

[英]Get value from URL in Javascript|jQuery and Symfony2 project

I need to get some parameters from URL using Javascript/jQuery and I found this nice function: 我需要使用Javascript / jQuery从URL获取一些参数,并且发现了这个不错的功能:

function getURLParameter(sParam) {
    var sPageURL = window.location.search.substring(1),
        sURLVariables = sPageURL.split('&');

    for (var i = 0; i < sURLVariables.length; i++) {
        var sParameterName = sURLVariables[i].split('=');
        if (sParameterName[0] == sParam) {
            return sParameterName[1];
        }
    }
};

So I started to use in my code but I'm having some issues since the parameter I'm looking for comes undefined . 因此,我开始在代码中使用它,但是由于要查找的参数undefined我遇到了一些问题。 First, this is a Symfony2 project so Profiler gives me this information: 首先,这是一个Symfony2项目,因此Profiler为我提供了以下信息:

Request Attributes

Key                             Value
_route_params                   [registro => 1, solicitud => 58]
...
registro                        1
solicitud                       58

What I need from here is registro and solicitud . 我在这里需要的是registrosolicitud So what I did at Javascript side was this: 所以我在Javascript方面所做的是:

console.log(getURLParameter('registro')); // gets undefined

But surprise I get undefined and I think the cause is that registro is not present at URL which is http://project.dev/app_dev.php/proceso/1/58/modificar but it's present in the REQUEST. 但是令人惊讶的是我undefined ,我认为原因是registrohttp://project.dev/app_dev.php/proceso/1/58/modificar URL中不存在,但在REQUEST中却存在。 How I can get the registro and solicitud values from whithin Javascript? 我怎样才能获得registrosolicitud从whithin的Javascript值? I need to send those parameters to a Ajax call, any advice? 我需要将这些参数发送给Ajax调用,有什么建议吗?

Try using this function: 尝试使用此功能:

function getParameters(){
    var url = window.location.href; //get the current url
    var urlSegment = url.substr(url.indexOf('proceso/')); //copy the last part
    var params = urlSegment.split('/'); //get an array
    return {registro: params[1], solicitud: params[2]}; //return an object with registro and solicitud
}

Then you can call the function and use the values: 然后,您可以调用该函数并使用值:

var params = getParameters();
console.log(params.registro);
console.log(params.solicitud);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM