<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>
/ video / metadata (mason组件)
<%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给出parserserror
答案 0 :(得分:0)
以防某些人拥有相同的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>
/ video / metadata (mason组件)
<%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 %>
希望它能为您节省几个小时