I am trying to update a DOM element with HTML generated from a symfony partial. I am using Symfony 1.3.8
Here is the (simplified) Symfony/PHP part:
public function executeFoobar(sfWebRequest $request)
{
$results = $this->renderPartial('foo/bar');
$this->getResponse()->setContentType('text/json');
return $this->renderText(json_encode(array('data' => $results)));
}
Here is the (simplified) HTML/jQuery part
<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>
When I submit the AJAX post, the html is generated server side, but I then get the error on the browser with the following message:
Error: Invalid JSON: <table>
<tr><!-- Rest of generated HTML follows .... -->
{"data": GENERATED_HTML }
Where GENERATED_HTML is the HTML generated at the server side – i.e.:
<table>
<tr><!-- Rest of generated HTML follows .... -->
So it seems the HTML is being included twice or something. Has anyone come accross this before – what am I doing wrong?
renderPartialechos the text to screen. You need to useget_partialfrom thePartialHelperIt’s either
loadHelpersofloadHelperI can’t remember off the top my head, sorry.