<script language="JavaScript">
var result = {};
var data;
function load_metadata (id) {
$.ajax({
url: "/video/metadata",
type: "POST",
data: id,
cache: false,
//dataType: "json",
//contentType: 'application/json',
success: function(data) {
result = data;
},
error: function(e, xhr){
alert('slideshow data load error: ');
}
});
alert(result.toSource()); //({})
}
</script>
/视频/元数据(梅森组件(
<%class>
use JSON;
has 'data';
</%class>
<%init>
use Data::Dumper;
print STDERR Dumper($.data); # $VAR1 = '"815"'
my $item_hash->{'item'} = "test"; # Valid JSON in JSONLint.com
my $json = to_json($item_hash, { ascii => 1 });
print STDERR Dumper($json); # $VAR1 = '{"item":"test"}';
</%init>
<% $json %>
它返回页面的每个html元素,而不是组件调用中的json
指定数据类型/contentType会产生解析错误
以防有人拥有相同的pb
忘记添加
<%flags>
extends => undef
</%flags>
inherit => undef
标志,以便循环组件继承,
取消注释dataType和
添加'async': false,
所以最后的工作代码是:
<script language="JavaScript">
var result = {};
function load_metadata (id) {
$.ajax({
url: "/video/metadata",
type: "POST",
data: id,
cache: false,
dataType: "json",
'async': false,
success: function(data) {
result = data;
},
error: function(e, xhr){
alert('slideshow data load error: ');
}
});
alert(result.toSource()); //({"item":"test"})
}
</script>
/视频/元数据(梅森组件(
<%flags>
extends => undef
</%flags>
<%class>
use JSON;
has 'data';
</%class>
<%init>
use Data::Dumper;
print STDERR Dumper($.data); # $VAR1 = '"815"'
my $item_hash->{'item'} = "test"; # Valid JSON in JSONLint.com
my $json = to_json($item_hash, { ascii => 1 });
print STDERR Dumper($json); # $VAR1 = '{"item":"test"}';
</%init>
<% $json %>
希望它能为您节省几个小时