2017-04-02 97 views
-1

我想对我的网址,以获取非常具体的字符串,在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 

我想只得到GUID afrer Token=像:

e12009cf-d48d-42e7-ba43-83b5082019bb 

我尝试使用:

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

,但我得到

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

我尝试过:

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

,但我得到

Uncaught ReferenceError: url is not defined

我需要做的令牌后,只得到参数是什么=?

我尝试,因为这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); 
     } 

但我得到一个错误:

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)

+1

的可能重复[我怎样才能得到查询字符串JavaScript中的值?](http://stackoverflow.com/questions/901115/how-can-i-get-query-string-values-in-javascript) – Archer

+0

我看过了,但是我不知道该如何实现它@Archer – Luis

+0

我试试它作为你的参考,但我仍然收到错误@Archer – Luis

回答

1

只是试试这个:

var url = window.location.hash.split('?Token=')[1]; 
var guid = url || ''; 
+0

问题是该标记是动态的,所以它可以改变每一个帖子,所以我需要阅读它,然后申请URL @ – Luis

+1

@Luis,Hajji的回答是有效的。请在判断前试用 – Mojtaba

+0

是的,现在可以使用。非常感谢你!! – Luis