gpt4 book ai didi

jquery - 使用 JQuery + JSON + Symfony 用 HTML 更新页面元素

转载 作者:行者123 更新时间:2023-12-01 03:25:59 24 4
gpt4 key购买 nike

我正在尝试使用从 symfony 部分生成的 HTML 来更新 DOM 元素。我使用的是 Symfony 1.3.8

这是(简化的)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 被包含了两次或者其他什么。以前有人遇到过这个问题吗?我做错了什么?

最佳答案

renderPartial 将文本回显到屏幕。您需要使用 PartialHelper

中的 get_partial
public function executeFoobar(sfWebRequest $request)
{
$this->getContext()->getConfiguration()->loadHelpers('Partial');

$results = get_partial('foo/bar');
$this->getResponse()->setContentType('text/json');
return $this->renderText(json_encode(array('data' => $results)));
}

它是 loadHelpersloadHelper 我记不清了,抱歉。

关于jquery - 使用 JQuery + JSON + Symfony 用 HTML 更新页面元素,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5165978/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com