Javascript json
does anyone know what this is not working? i have been trying for days now.
function loadContent(obj, getcmt)
{
var params = $(obj).attr('href').split('?');
$.get(BASE_DIR+'co开发者_如何学运维ntent/load.php?'+params[1],
function(json) {
var result = eval('('+json+')');
if (result.returnval == 1) {
$('#content').fadeOut('fast', function() { $(this).html(result.content).fadeIn('slow'); });
}
});
return false;
}
Firebug just give an error "missing } after property list" and link to jquery. and another debugger show json as the reason why.
any help is greatly appreciated!
EDIT:
The top work fine but the following dont work:
<div id="content">
<a href="{$content.item_url}" onclick="return loadContent2(this, 'page', '2')">
</a>
</div>
how ever if the single quote in onClick is not there, it work. like so:
<div id="content">
<a href="{$content.item_url}" onclick="return loadContent2(this, "page", "2")">
</a>
</div>
However the function in question loadContent2 does not work.
Try it using jQuery's built-in tools:
function loadContent(obj, getcmt) {
var params = $(obj).attr('href').split('?');
$.getJSON(BASE_DIR+'content/load.php?'+params[1], function(json) {
if (json.returnval == 1) {
$('#content').fadeOut('fast', function() {
$(this).html(json.content).fadeIn('slow');
});
}
});
return false;
}
This uses $.getJSON()
which is awesome.
if that has problems, check your response:
$.getJSON(BASE_DIR+'content/load.php?'+params[1], function(json) {
console.debug(json);
});
精彩评论