2017-06-29 103 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的分配。 –

相关问题