我正在尝试设置引导类型头。这是我的 jquery 代码
$(function () {
$.ajax({
type: "GET",
url: "http://example.com/search?callback=my_callback",
data: { keyword: 'r' },
jsonpCallback: "my_callback",
dataType: "jsonp",
error: function (xhr, errorType, exception) {
var errorMessage = exception || xhr.statusText;
alert("Excep:: " + exception + "Status:: " + xhr.statusText);
}
});
var sources = [
{ name: "local", type: "localStorage", key: "cities", display: "country" },
{ name: "remote", type: "remote", url: "/cities/list" },
{ name: "data", type: "data", data: [] }
];
$('input.typeahead.local.remote').typeahead({
sources: [
{ name: "local", type: "localStorage", key: "cities" }
]
});
});
function my_callback(data) {
alert(data.count);
var src2 = '[';
var i;
for (i = 0; i < data.count; i++) {
src2 = src2 + "{ id : " + (i + 1) + " , name: '" + data.response[i].name + "' },";
}
src2 = src2.substring(0, src2.length - 1);
src2 = src2 + ']';
var sampleJson = [{ id: 1, name: 'Really Awesome' }, { id: 2, name: 'RSpecge'}];
localStorage.setItem("cities", JSON.stringify(src2));
}
在我的回调函数中,当我localStorage
使用从 jquery ajax 调用返回的数据进行设置时,它不起作用。但是当我尝试直接使用变量设置数据时,sampleJson
它起作用了。是这样吗??
这是从 jquery ajax 的响应动态创建的 json,它看起来与 sampleJson 相同
[{ id: 1, name: 'Really Awesome' }, { id: 2, name: 'RSpecge'}]
这是sampleJson
var sampleJson = [{ id: 1, name: 'Really Awesome' }, { id: 2, name: 'RSpecge'}];