简体   繁体   中英

jQuery parseJSON and functions inside the json string

I'm trying the following:

var objJson = jQuery.parseJSON('"myFunc": function(){ ... }');

This fails. Now my question(s): Why does it fail and how can I achieve what I'm trying?

Thank you in advance!

You seem to have a misconception about what JSON is. JSON is a simple data format with a syntax that resembles JavaScript objects. It's so simple that spec fits a single page. If you have functions it it, it isn't JSON. Thus there is no point in using JSON tools.

Now, the usual way to inject external JavaScript code into an HTML document is the <script> tag :

<script type="text/javascript" src="/path/to/code.js">

好的,以下对我有用(应该用谷歌搜索更长的时间):

var o = eval('(' + '"myFunc": function(){ ... }' + ')')

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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