2017-06-29 102 views
0

I'm收到此錯誤strace的遺漏的類型錯誤:無法讀取屬性 '子'

Uncaught TypeError: Cannot read property 'substring' of undefined

這就是我想要

http://localhost:3001/product/?id=5

獲取的 「5」

我是在Javascript中使用這種代碼

var id = url.substring(url.lastIndexOf('?=') + 1); 

    var url = "http://localhost:3000/product/"+id; 
     console.log(url); 
    $.ajax({ 
      url:url, 

......... 
}); 

ID = 5:161遺漏的類型錯誤:未定義

+0

你定義'url' *** ***後,你嘗試使用它... –

+0

檢查答案。組織良好,按預期工作。 –

回答

0

你只需要這樣的代碼來declarated

var url = "http://localhost:300123/temp/?id=5"; 
 
    var id = url.substring(url.lastIndexOf('=')+1); 
 
    var newUrl = "http://localhost:3001/product/?id="+id; 
 
    console.log(newUrl);

然後,你可以做你的進一步操作

$.ajax({ 
      url:newUrl , 

......... 
}); 
+0

'$ .url()'不是一個標準的jQuery方法 –

+0

感謝@RoryMcCrossan糾正我。 –

+0

不工作,我得到http:// localhost:3000/product/http:// localhost:3001/product /我想要id與concatenet與localhost:3000:product/< - id – Scripters

0

無法讀取屬性 '子' 你url應當在其使用

var url = location.href; // just guess, how you use it 
var newUrl = "http://localhost:3000/product/"; 
var id = url.substring(url.lastIndexOf('=') + 1); 
newUrl += id; 

console.log(url, newUrl); 
$.ajax({ 
    url:newUrl, 
    ......... 
}); 

編輯之前的

+0

並且您正在添加標識而未聲明? – Scripters

+0

對不起,我沒有注意到url中id的分配。 –

相關問題