admin管理员组文章数量:1278983
In the root of my .js
file I declare:
var Doe = {
"foo": "The quick brown fox jumps over the lazy dog.",
"bar": "ABCDEFG",
"baz": [52, 97],
"testfunc": function(){alert('json function');}
}
And in my HTML:
<a id="lele" href="javascript:void(0)" onclick="Doe.testfunc()">Doe</a>
And when I click it it works, so I know it's possible to put functions in JSON objets.
Now, in the root of the .js
I declare:
$.getJSON("~/rule/2", function(data) {
alert("2: " + data.foo);
data.testfunc();
});
And the server is returning this for that url:
context.Response.ContentType = "application/json";
context.Response.Output.Write(@"{
""foo"": ""The quick brown fox jumps over the lazy dog."",
""bar"": ""ABCDEFG"",
""baz"": [52, 97],
""testfunc"": function(){alert('json function');}
}");
context.Response.Flush();
But nothing happens :( Aparently because if there is an error, .getJSON will fail silently. But, if I remove the lines regarding of the function:
$.getJSON("~/rule/2", function(data) {
alert("2: " + data.foo);
});
and
context.Response.Output.Write(@"{
""foo"": ""The quick brown fox jumps over the lazy dog."",
""bar"": ""ABCDEFG"",
""baz"": [52, 97]}");
Then it works, an alert pop up with the content of the "foo" property.
What is the problem with the function? :( In the moment that I put a function in the JSON object, jQuery fails.
Kind regards.
In the root of my .js
file I declare:
var Doe = {
"foo": "The quick brown fox jumps over the lazy dog.",
"bar": "ABCDEFG",
"baz": [52, 97],
"testfunc": function(){alert('json function');}
}
And in my HTML:
<a id="lele" href="javascript:void(0)" onclick="Doe.testfunc()">Doe</a>
And when I click it it works, so I know it's possible to put functions in JSON objets.
Now, in the root of the .js
I declare:
$.getJSON("~/rule/2", function(data) {
alert("2: " + data.foo);
data.testfunc();
});
And the server is returning this for that url:
context.Response.ContentType = "application/json";
context.Response.Output.Write(@"{
""foo"": ""The quick brown fox jumps over the lazy dog."",
""bar"": ""ABCDEFG"",
""baz"": [52, 97],
""testfunc"": function(){alert('json function');}
}");
context.Response.Flush();
But nothing happens :( Aparently because if there is an error, .getJSON will fail silently. But, if I remove the lines regarding of the function:
$.getJSON("~/rule/2", function(data) {
alert("2: " + data.foo);
});
and
context.Response.Output.Write(@"{
""foo"": ""The quick brown fox jumps over the lazy dog."",
""bar"": ""ABCDEFG"",
""baz"": [52, 97]}");
Then it works, an alert pop up with the content of the "foo" property.
What is the problem with the function? :( In the moment that I put a function in the JSON object, jQuery fails.
Kind regards.
Share Improve this question asked Jan 13, 2011 at 17:56 vtortolavtortola 36k31 gold badges167 silver badges268 bronze badges3 Answers
Reset to default 6You don't have a JSON object.
You have an object literal. Functions cannot be values of a JSON string.
JSON is only a subset of the object literal syntax. Possible values are:
(source: json)
Read more on json
This is JavaScript object:
var foo = {bar: 42}
and this is a JSON string:
var foo = '{"bar": 42}';
See also JavaScript, JSON objects and object literal notation.
Update: In order to return and execute JavaScript, you can do the following:
You can return plain JavaScript e.g.
context.Response.ContentType = "text/javascript";
context.Response.Output.Write(@"
function testfunc(){
alert('json function')
};
testfunc();
");
and use $.ajax
with dataType: 'script'
:
$.ajax({
url: "~/rule/2",
dataType: 'script',
success: function(data) {}
});
DEMO
You can also define JavaScript objects with functions like so:
context.Response.ContentType = "text/javascript";
context.Response.Output.Write(@"
var obj = {
bar: function(){alert(42);}
}
yourHandler(obj);
");
You just need to have a global function called yourHandler
(and of course still call ajax
with dataType: script
).
This is probably what es closest to what you want. The difference is, that you don't return JSON, but JavaScript, and you don't process the object in the success
callback, but in another function, which has to be called from within the JS you sent.
See another DEMO
No it's not possible to put functions in JSON objects. It's possible to put functions in JavaScript objects though, which is what you've demonstrated in the first snippet.
You could place just string representation the function body in your JSON that you're sending, then give that data to the Function
constructor.
Example: http://jsfiddle/m8PQY/1/
var myJSON = '{"func":"{alert(\'json function\');}"}';
var obj = $.parseJSON( myJSON );
var myFunc = new Function( obj.func );
myFunc();
Obviously you would only want to do this if you are the author of the code you're sending.
本文标签: javascriptGetting JSON with functions with jQueryStack Overflow
版权声明:本文标题:javascript - Getting JSON with functions with jQuery - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741254069a2366333.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论