我想用symfony部分生成的HTML更新DOM元素。我使用的Symfony 1.3.8使用JQuery + JSON + Symfony更新HTML頁面元素
這裏是(簡化)的Symfony/PHP部分:
public function executeFoobar(sfWebRequest $request)
{
$results = $this->renderPartial('foo/bar');
$this->getResponse()->setContentType('text/json');
return $this->renderText(json_encode(array('data' => $results)));
}
這裏是(簡化)HTML/jQuery的一部分
<html>
<head><script type="text/javascript" src="/js/jquery.js"></script>
<body>
<a id='foo' href='#'>Click foo</a>
<div id="sink"></div>
</body>
<script type="text/javascript">
$(document).ready(function(){
$('#foo).click(function(){
$.ajax({
type: 'POST',
url: '/ajax-example'
dataType: 'json',
success: function(result){ $('#sink).val(result.data); }
error: function(xhr, ajaxOptions, thrownError){ alert('Error: ' + thrownError); }
});
});
});
</script>
</html>
當我提交AJAX後,html是生成服務器端,但我然後在瀏覽器上收到以下消息的錯誤:
Error: Invalid JSON: <table>
<tr><!-- Rest of generated HTML follows .... -->
{"data": GENERATED_HTML }
凡GENERATED_HTML是在服務器端生成的HTML - :
<table>
<tr><!-- Rest of generated HTML follows .... -->
即如此看來被包含兩次或某事的HTML。有沒有人來過這個問題 - 我做錯了什麼?
現在你指出來。它的那麼明顯,以使我要掛我的恥辱頭;) – skyeagle 2011-03-02 10:58:57
NP,我做我的最好幫助解決SO – xzyfer 2011-03-02 11:00:06