Node.js URL路径包含未转换的字符
我得到以下错误。我知道我需要逃避一些字符,但有没有更好的方法来做到这一点 我的path变量有一些错误Node.js URL路径包含未转换的字符,node.js,http,Node.js,Http,我得到以下错误。我知道我需要逃避一些字符,但有没有更好的方法来做到这一点 我的path变量有一些错误 getNearByUsers(latlong){ 返回http.get({ 主机:“search-vegme-user-7l3rylms73566frh4hwxblekn4.us-east-1.cloudsearch.amazonaws.com”, 路径:'/2013-01-01/search?q=nikhil&expr.distance=haversin(35.621966,-120.686
getNearByUsers(latlong){
返回http.get({
主机:“search-vegme-user-7l3rylms73566frh4hwxblekn4.us-east-1.cloudsearch.amazonaws.com”,
路径:'/2013-01-01/search?q=nikhil&expr.distance=haversin(35.621966,-120.686706,latlong.latitude,latlong.longitude)&sort=distance asc&return=distance,displayname,profileimageurl'
},功能(回应){
//使用数据持续更新流
变量体=“”;
响应。关于('数据',函数(d){
body+=d;
});
on('end',function(){
//数据接收完成了,用它做任何事情!
var parsed=JSON.parse(body);
console.log(已解析);
});
});
}
“错误”:[
{
“消息”:“请求路径包含未转义字符。”,
“原始错误”:{}
}
]
您需要使用
改进的解决方案:
或使用。
var value = "haversin(35.621966,-120.686706,latlong.latitude,latlong.longitude)";
value = encodeURIComponent(value);
console.log(value); //"haversin(35.621966%2C-120.686706%2Clatlong.latitude%2Clatlong.longitude)"
serialize = function(obj) {
var str = [];
for(var p in obj)
if (obj.hasOwnProperty(p)) {
str.push(encodeURIComponent(p) + "=" + encodeURIComponent(obj[p]));
}
return str.join("&");
}
console.log("/2013-01-01/search?" + serialize({
'q': "nikhil",
'expr.distance': "haversin(35.621966,-120.686706,latlong.latitude,latlong.longitude)",
'sort': "distance asc",
'return': "distance,displayname,profileimageurl"
}));