-1

I want to get very specific string over my url to add as parameter in js:

 function cambiarContrasena(usuario, completado, fallo) {
            apiService.post('/api/usuario/cambiarContrasena?token=', usuario,
            completado,
            fallo);
        }

URL

http://localhost:55728/Cliente/#/cambiarContrasena.html?Token=e12009cf-d48d-42e7-ba43-83b5082019bb

I want to get only Guid afrer Token= like:

e12009cf-d48d-42e7-ba43-83b5082019bb

I try using:

   var url = (location.pathname + location.search).substr(1);
    function cambiarContrasena(usuario, completado, fallo) {
        apiService.post('/api/usuario/cambiarContrasena?token='+url, usuario,
        completado,
        fallo);
    }

But I get

http://localhost:55718/api/usuario/cambiarContrasena?token=Cliente/

I try too:

var guid = url.substr(url.indexOf('Token=') + 6);
        function cambiarContrasena(usuario, completado, fallo) {
            apiService.post('/api/usuario/cambiarContrasena?token='+guid, usuario,
            completado,
            fallo);
        }

But I get

Uncaught ReferenceError: url is not defined

What I need to do to get only parameters after Token= ?

I try as this question:

 function getParameterByName(name, url) {
            if (!url) {
                url = window.location.href;
            }
            name = name.replace(/[\[\]]/g, "\\$&");
            var regex = new RegExp("[?&]" + name + "(=([^&#]*)|&|#|$)"),
                results = regex.exec(url);
            if (!results) return null;
            if (!results[2]) return '';
            return decodeURIComponent(results[2].replace(/\+/g, " "));
        }
        function cambiarContrasena(usuario, completado, fallo) {
            apiService.post('/api/usuario/cambiarContrasena?token='+getParameterByName, usuario,
            completado,
            fallo);
        }

But I get an error:

POST http://localhost:55718/api/usuario/cambiarContrasena?token=function%20getPa…%20%20var%20regex%20=%20new%20RegExp(%22[?&]%22%20+%20name%20+%20%22(=([^& 400 (Bad Request)

Community
  • 1
  • 1
Luis
  • 47
  • 5

1 Answers1

1

just try this :

var url = window.location.hash.split('?Token=')[1];
var guid = url || '';
Hajji Tarik
  • 1,072
  • 7
  • 23