自动完成(jQuery UI)和localstorage [英] Autocomplete (jQuery UI) and localstorage

查看:172
本文介绍了自动完成(jQuery UI)和localstorage的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用这两个工具时,我发现了一个奇怪的错误。
我正在对API进行AJAX查询,然后检索存储在localStorage中并显示在自动完成面板中的JSON数据。问题是,根据自动完成源的来源,面板会有不同的反应。

I just get a strange bug when using those two tools. I'm making an AJAX query to an API, then retrieving JSON data which is stored within localStorage and displayed into an autocomplete panel. The problem is that according to the origin of the autocomplete source, the panel will react differently.

这是在AJAX成功时调用的回调函数:

Here is the callback function called on AJAX success :

function _company_names(data)
{
    localStorage.setItem('ac_source', JSON.parse(data).Result);

    // Works fine
    $("#search_input").autocomplete( "option", "source", JSON.parse(data).Result);
    // Send an AJAX request
    $("#search_input").autocomplete( "option", "source", localStorage.getItem('ac_source'));
}

如果我将JSON.parse(data).Result作为结果传递给自动完成来源,没关系。但是,如果我传递localStorage.getItem('ac_source'),ac小部件将发送一个AJAX请求(不使用我自己的函数)吹风(我的node.js将尝试解析它等)。

If i pass JSON.parse(data).Result as result to the autocomplete source, it will be fine. However if I pass localStorage.getItem('ac_source'), the ac widget will send an AJAX request (not using my own function) blowing in the wind (my node.js will try to parse it, etc.).

我正在使用localstorage从我的代码的其他部分访问这些数据(存储它以与其他用户研究进行比较并在请求相同时显示它)。

I'm using localstorage to access these data from an other part of my code (store it to compare with other user research and displaying it if the request is the same).

推荐答案

您只能在本地存储中存储字符串数据:

You can store only string data in Local Storage:

localStorage.setItem('ac_source', '{"key":"data","key1":"data1"}');

$("#search_input").autocomplete( "option", "source", JSON.parse(localStorage.getItem('ac_source')).Result);

这篇关于自动完成(jQuery UI)和localstorage的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆