使用jquery解析安全json? [英] Safe json parsing with jquery?

查看:99
本文介绍了使用jquery解析安全json?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用json与json。我的客户端页面生成json,我存储在我的服务器上。然后客户端可以稍后再取出json,解析并显示它。

I am using jquery with json. My client pages generate json, which I store on my server. The clients can then fetch the json back out later, parse, and show it.

由于我的客户端正在生成json,因此可能不安全。我认为jquery在内部使用eval()。真的吗?有没有办法在可用的浏览器中使用本机json解析器,否则会回退到手动解析?我是jquery的新手,所以我不知道在哪里插入自己的解析代码。我正在做类似的事情:

Since my clients are generating the json, it may not be safe. I think jquery uses eval() internally. Is that true? Is there a way to use the native json parsers from the browsers where available, otherwise fall back to manual parsing if not? I'm new to jquery so I don't know where I'd insert my own parsing code. I'm doing something like:

$.ajax({
    url: 'myservlet',
    type: 'GET',
    dataType: 'json',
    timeout: 1000,
    error: function(){
        alert('Error loading JSON');
    },
    success: function(json){
        alert("It worked!: " + json.name + ", " + json.grade);
    }
});

所以在success()方法中,json对象已经为我解析了。有没有办法先将它作为原始字符串捕获?然后我可以决定是使用原生解析器还是手动解析(希望有一个jquery插件...)。

so in the success() method, the json object is already parsed for me. Is there a way to catch it as a raw string first? Then I can decide whether to use the native parsers or manual parsing (hoping there's a jquery plugin for that..).

我正在阅读的文章都是来自不同的年,所以我不知道jquery是否已经为json放弃了eval(),

The articles I'm reading are all from different years, so I don't know if jquery has already abandoned eval() already for json,

谢谢

推荐答案

最新版本有 jQuery.parseJSON 。它将在拥有它的浏览器中使用本机JSON。对于较旧的,它将进行正则表达式健全性检查,然后使用新的Function()(基本上是eval)。

The latest version has jQuery.parseJSON. It will use native JSON in browsers that have it. For older ones, it will do a regex sanity check, then use new Function() (basically eval).

由于您指定了'json'作为dataType,它将在这里使用parseJSON。这是在内部 httpData函数中处理的

Since you specified 'json' as the dataType, it will use parseJSON here. This is handled in the internal httpData function

这篇关于使用jquery解析安全json?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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