2017-04-03 154 views
1

我有這個錯誤,我谷歌關於它。但我沒有發現我的問題未捕獲TypeError:無法讀取未定義窗口位置的屬性'split'

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

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

我的價值不是空的或未定,任何人都可以幫我嗎?謝謝!

+0

請出示的console.log的'值(window.location.hash);?' – 4castle

+0

It's'#/ cambiarContrasena.html令牌= e12009cf-d48d-42e7-ba43-83b5082019bb' @ 4castle – Luis

+0

你確定錯誤不在第二行 – adeneo

回答

1

使用href而不是hashhashhref之間

var url = window.location.href.split('?Token=')[1]; 
// url = url.split('Token='); no need to split it again. 

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

區別是他們返回URL的不同部分。下面是URL的解剖:

Anatomy of a URL

在這種情況下window.location.hash回報#00h02m30s其中windows.location.href回報整個URL。

+0

相同問題未捕獲TypeError:無法讀取undefined的屬性'split' – Luis

+0

向我們顯示console.log(location.href) –

+0

http:// localhost:55718/Cliente /並且應該是http:// localhost:55718/Cliente /# /login.html – Luis

0

我想應該是var url = window.location.split('?Token=')[1];(不.hash因爲要拍攝參數)

+0

如果我不使用散列,我得到Uncaught TypeError:window.location.split不是函數 – Luis

+0

我的錯誤,應該是'.href',請參閱@Nobody的答案。 –

相關問題