Rails 返回hash给javascript
发布日期:2021-09-29 20:09:20 浏览次数:9 分类:技术文章

本文共 4928 字,大约阅读时间需要 16 分钟。

这是一个特别的,不太正统的需求,
因为,大部分时候,ajax的返回,只要返回json类型数据就可以了
然而,如果返回的是partial,partial里有变量是hash,而这个hash又需要jquery的plugin例如图表之类的用,那就需要这个rails的hash转成javascript能认,能读的hash了。
思路呢,还是通过json
就是在haml或者erb页面里吧hash转json
然后
jQuery.parseJSON( json )
var obj = jQuery.parseJSON('{"name":"John"}'); alert( obj.name === "John" );
或者
eval
另外,这样的转换,需要比较标准的json格式,要一般的转换参考用下面的
[url]https://github.com/douglascrockford/JSON-js[/url]
[quote]JavaScript is a general purpose programming language that was introduced as the page scripting language for Netscape Navigator. It is still widely believed to be a subset of Java, but it is not. It is a Scheme-like language with C-like syntax and soft objects. JavaScript was standardized in the ECMAScript Language Specification, Third Edition.
JSON is a subset of the object literal notation of JavaScript. Since JSON is a subset of JavaScript, it can be used in the language with no muss or fuss.
var myJSONObject = {"bindings": [
{"ircEvent": "PRIVMSG", "method": "newURI", "regex": "^http://.*"},
{"ircEvent": "PRIVMSG", "method": "deleteURI", "regex": "^delete.*"},
{"ircEvent": "PRIVMSG", "method": "randomURI", "regex": "^random.*"}
]
};
In this example, an object is created containing a single member "bindings", which contains an array containing three objects, each containing "ircEvent", "method", and "regex" members.
Members can be retrieved using dot or subscript operators.
myJSONObject.bindings[0].method // "newURI"
To convert a JSON text into an object, you can use the eval() function. eval() invokes the JavaScript compiler. Since JSON is a proper subset of JavaScript, the compiler will correctly parse the text and produce an object structure. The text must be wrapped in parens to avoid tripping on an ambiguity in JavaScript's syntax.
var myObject = eval('(' + myJSONtext + ')');
The eval function is very fast. However, it can compile and execute any JavaScript program, so there can be security issues. The use of eval is indicated when the source is trusted and competent. It is much safer to use a JSON parser. In web applications over XMLHttpRequest, communication is permitted only to the same origin that provide that page, so it is trusted. But it might not be competent. If the server is not rigorous in its JSON encoding, or if it does not scrupulously validate all of its inputs, then it could deliver invalid JSON text that could be carrying dangerous script. The eval function would execute the script, unleashing its malice.
To defend against this, a JSON parser should be used. A JSON parser will recognize only JSON text, rejecting all scripts. In browsers that provide native JSON support, JSON parsers are also much faster than eval. It is expected that native JSON support will be included in the next ECMAScript standard.
var myObject = JSON.parse(myJSONtext, reviver);
The optional reviver parameter is a function that will be called for every key and value at every level of the final result. Each value will be replaced by the result of the reviver function. This can be used to reform generic objects into instances of pseudoclasses, or to transform date strings into Date objects.
myData = JSON.parse(text, function (key, value) {
var type;
if (value && typeof value === 'object') {
type = value.type;
if (typeof type === 'string' && typeof window[type] === 'function') {
return new (window[type])(value);
}
}
return value;
});
A JSON stringifier goes in the opposite direction, converting JavaScript data structures into JSON text. JSON does not support cyclic data structures, so be careful to not give cyclical structures to the JSON stringifier.
var myJSONText = JSON.stringify(myObject, replacer);
If the stringify method sees an object that contains a toJSON method, it calls that method, and stringifies the value returned. This allows an object to determine its own JSON representation.
The stringifier method can take an optional array of strings. These strings are used to select the properties that will be included in the JSON text.
The stringifier method can take an optional replacer function. It will be called after the toJSON method (if there is one) on each of the values in the structure. It will be passed each key and value as parameters, and this will be bound to object holding the key. The value returned will be stringified.
Values that do not have a representation in JSON (such as functions and undefined) are excluded.
Nonfinite numbers are replaced with null. To substitute other values, you could use a replacer function like this:
function replacer(key, value) {
if (typeof value === 'number' && !isFinite(value)) {
return String(value);
}
return value;
}
[/quote]
var myJSONObject = {"bindings": [         {"ircEvent": "PRIVMSG", "method": "newURI", "regex": "^http://.*"},         {"ircEvent": "PRIVMSG", "method": "deleteURI", "regex": "^delete.*"},         {"ircEvent": "PRIVMSG", "method": "randomURI", "regex": "^random.*"}     ] };

转载地址:https://blog.csdn.net/horace_lee/article/details/84085339 如侵犯您的版权,请留言回复原文章的地址,我们会给您删除此文章,给您带来不便请您谅解!

上一篇:一个ajax和rails交互的例子
下一篇:关于Rubymine

发表评论

最新留言

路过,博主的博客真漂亮。。
[***.116.15.85]2024年04月04日 21时29分21秒

关于作者

    喝酒易醉,品茶养心,人生如梦,品茶悟道,何以解忧?唯有杜康!
-- 愿君每日到此一游!

推荐文章