我看到我的本地存储在页面上。我试图拉入user_id并将其放在数据URL中以构建我的查询。有人可以让我知道我做错了什么?
<!--GET REMOTE JSON DATA-->
<script>
var app = new kendo.mobile.Application();
var theuser = localStorage.getItem('user_ID'); //grab user ID from local storage
var IRISalerts = new kendo.data.DataSource({
transport: {
read: {
url: "http://procdev.irisdispatch.com/ws/mobilefunctions.cfc? method=getAlerts&user_id='+theuser'",
dataType: "json", // "jsonp" is required for cross-domain requests; use "json" for same-domain requests
}
},
答案 0 :(得分:0)
您不能使用.encodeURIComponent()
对您的查询参数进行编码,不要这样做;它可能有无效的网址字符(特殊字符等)让你头疼。尝试这个功能,你可以用它作为对象 - &gt;查询字符串转换(如果在存储中存储json objs),它对key = value对也表现良好。对于单个查询参数,.encodeURIComponent()
完成工作......
function uriParamNcoder(obj) {
if (
Object.prototype.toString.call(obj) === "[object String]"
) {
return obj.split('=')
.map(
function (part) {
return encodeURIComponent(part);
}
)
.join('=')
.replace(/%20/g, '+');
}
var q_str_arr = [];
objTrace(
obj,
function (k, v, trace) {
var tmp = trace.match(/^\[(.+?)\](.*)$/);
q_str_arr.push(
encodeURIComponent(tmp[1]) +
((tmp[2] !== void 0) ? encodeURIComponent(tmp[2]) : "") + "=" + encodeURIComponent(v)
);
}
);
function objTrace(obj, fn) {
if (
obj === Object(obj) && (typeof fn === "function")
) {
objParamTracer.apply(
obj, [obj, fn].concat(Array.prototype.slice.call(arguments, 2))
);
}
return obj;
}
function objParamTracer(obj, fn, trace) {
trace || (trace = "");
if (
Object.prototype.toString.call(obj) === "[object Array]"
) {
obj.forEach(
function (o, k) {
if (o === Object(o)) {
return objParamTracer.apply(
o, [o, fn].concat(trace + "[]")
);
} else {
return fn.apply(
this, [k, o].concat(trace + "[]")
);
}
},
obj
);
} else {
ownEach(
obj,
function (p, o) {
if (
o === Object(o)
) {
return objParamTracer.apply(
obj, [o, fn].concat(trace + "[" + p + "]")
);
} else {
return fn.apply(
this, [p, o].concat(trace + "[" + p + "]")
);
}
}
);
}
}
function ownEach(obj, fn) {
for (var prop in obj) {
if (obj.hasOwnProperty(prop)) {
fn.call(obj, prop, obj[prop]);
}
}
return obj;
}
return q_str_arr.join('&').replace(/%20/g, '+');
}