简体   繁体   English

JSON值中的jQuery对象

[英]jQuery object in JSON value

Here be my first question! 这是我的第一个问题!

I'm calling JSON via AJAX for use in a client side validation library. 我正在通过AJAX调用JSON,以在客户端验证库中使用。 The library calls for me to specify where, for the current rule, I wish to insert the error message. 该库要求我为当前规则指定我希望在何处插入错误消息。 For my static validation rules I can write an object such as: 对于我的静态验证规则,我可以编写一个对象,例如:

{ 'errorlocation' : jQuery('[name="foo"]').closest('.bar') }

My validation library allows me to inject more rules after the first init, so when I submit my model by REST I could receive a server response with errors. 我的验证库允许我在第一次初始化后注入更多规则,因此当我通过REST提交我的模型时,我会收到带有错误的服务器响应。 I am currently developing that server response. 我目前正在开发该服务器响应。 I would prefer that the error response would simply be an array of validation rules so that I can later inject those rules into the same validation object that has already been created on my page. 我希望错误响应只是一个验证规则数组,以便以后可以将这些规则注入到已经在页面上创建的相同验证对象中。 So within the response JSON I would require such a definition as that in the code snippet above. 因此,在响应JSON中,我将需要上述代码段中的定义。

It seems though that Backbone and jQuery's parse json algorithms can not evaluate the jQuery() value of the object even though jQuery is definitely available in the global namespace. 似乎Backbone和jQuery的parse json算法无法评估对象的jQuery()值,即使jQuery在全局名称空间中肯定可用。 So this doesn't work: 所以这不起作用:

jQuery.parseJSON("{ 'errorlocation' : jQuery('[name=\"foo\"]').closest('.bar') }");

I have thought about just storing the "[name='foo']" and jQuerying after the parse but that doesn't work for the case where I want to chain, such as that above .closest('.bar') 我曾想过只在解析后存储“ [name ='foo']”和jQuerying,但这不适用于我想链接的情况,例如上面的.closest('.bar')

Does anyone know of a JSON parser that will parse/allow variables located in the global namespace? 有谁知道JSON解析器将解析/允许位于全局命名空间中的变量?

JSON is a data format, not a code format. JSON是数据格式,而不是代码格式。 Generally you want to structure your system in a way that you're passing data from the server to the client, and then your client uses the data to determine an action. 通常,您希望以一种将数据从服务器传递到客户端的方式来构造系统,然后客户端使用数据来确定操作。 So maybe you would pass the foo and .bar back as data, and another parameter to determine what function to run. 因此,也许您会将foo.bar作为数据传递回去,并通过另一个参数确定要运行的函数。

If you really HAVE to design it this way, and I advise against that, you can use eval to evaluate an arbitrary string as javascript, like this: 如果您真的必须以这种方式设计它,而我建议您这样做,则可以使用eval将任意字符串评估为javascript,如下所示:

var errorInfo = jQuery.parseJSON(
    "{ 'errorlocation' : \"jQuery('[name=\"foo\"]').closest('.bar')\" }");

eval(errorInfo.errorocation);

Have you considered storing the whole jQuery call as a string, then running eval in the client side? 您是否考虑过将整个jQuery调用存储为字符串,然后在客户端运行eval? Like this: 像这样:

json = jQuery.parseJSON("{ 'errorlocation' : 'jQuery(\'[name=\"foo\"]\').closest('.bar')' }");
location = eval(json.errorlocation) //runs the jQuery string

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM