gpt4 book ai didi

javascript - Symfony 和 Ajax 错误 404,Symfony 工具栏中没有错误消息

转载 作者:行者123 更新时间:2023-12-03 04:47:47 27 4
gpt4 key购买 nike

我正在使用 Symfony 开发 Web 应用程序,但在使用 Ajax 时遇到一些问题。

我想按一些带有 id (1,2,3,4...) 的按钮,并在我的数据库中动态(使用 ajax)搜索一些与这些 id 相关的内容。

我的 JavaScript 代码:

appP08.controls.btnZone.on('click', function(){
var ZoneID = $(this).attr('id');
var ZoneName = $(this).text();
alert(ZoneID);

$.ajax({
url: "/recor/circuit",
type: "POST",
data: {
ZoneID: ZoneID
},
success: function(data){
$('#divCircuit').html(data);
$('#zoneChoice').text(' - ' + ZoneName);
appP08.controls.divZone.hide();
}
});
});

当我按下按钮时,Ajax 就会被执行,并且我会看到警报,因此 Jquery 运行良好。但之后什么也没有发生。

我已经创建了一条路线:

mk_reappro_production_circuit:
path: /recor/circuit
defaults: {_controller: MKReapproProductionBundle:Appli:circuit }
methods: [post]

和 Controller :

    public function circuitAction(Request $req){
$req = $this->getRequest();
if ($req->isXMLHttpResult()){
$id = $req->request->get('ZoneID');

if($id != null){
$repository = $this->getDoctrine()->getManager()->getRepository('MKReapproProductionBundle:Circuit');
$listCircuits = $repository->findAll($id);

return $this->render('MKReapproProductionBundle:Appli:circuit.html.twig', array(
'listCircuits' => $listCircuits
));
}
}
}

View :

{% extends "MKReapproProductionBundle::layout.html.twig" %}

{% block mk_appli_train_body_circuit %}

<div id="divCircuit">
{% for circuit in listCircuits %}
<button class="btn btn-success" name="btnZone">
{{ circuit.name }}
</button>
{% endfor %}
</div>

{% endblock %}

我应该怎么做才能显示这个按钮?在 Symfony 工具栏中只有这个: Symfony Toolbar Message Error

最佳答案

我认为这个错误是因为 Symfony 没有找到路径,因为你没有正确生成它。试试这个:

appP08.controls.btnZone.on('click', function(){
var ZoneID = $(this).attr('id');
var ZoneName = $(this).text();
alert(ZoneID);

$.ajax({
url: "{{ path('mk_reappro_production_circuit') }}", //Change this line.
type: "POST",
data: {
ZoneID: ZoneID
},
success: function(data){
$('#divCircuit').html(data);
$('#zoneChoice').text(' - ' + ZoneName);
appP08.controls.divZone.hide();
}
});
});

使用 {{ path('mk_reappro_Production_Circuit') }},Symfony 将负责生成正确的路径。

作为附加评论,在 Controller 中:

    public function circuitAction(Request $req){
$req = $this->getRequest(); //This line is not necessary, you are passing the $req variable directly to the function.
if ($req->isXMLHttpResult()){
$id = $req->request->get('ZoneID');

if($id != null){
$repository = $this->getDoctrine()->getManager()->getRepository('MKReapproProductionBundle:Circuit');
$listCircuits = $repository->findAll($id);

return $this->render('MKReapproProductionBundle:Appli:circuit.html.twig', array(
'listCircuits' => $listCircuits
));
}
}
}

已编辑:

当然,{{ path('mk_reappro_Production_Circuit') }} 在 twig 模板内工作,因为 path 是一个 twig 变量。如果您要导入外部 js 文件,则必须在 twig 模板中创建一个可以在 js 中使用的全局变量。像这样的事情:

{% block js %}
<script type="text/javascript">
var path = "{{ path('mk_reappro_production_circuit') }}";
</script>
{% endblock %}
该语句必须位于您的 js 文件之前。

现在,在你的 js 中:

appP08.controls.btnZone.on('click', function(){
var ZoneID = $(this).attr('id');
var ZoneName = $(this).text();
alert(ZoneID);

$.ajax({
url: path, //Change this line.
type: "POST",
data: {
ZoneID: ZoneID
},
success: function(data){
$('#divCircuit').html(data);
$('#zoneChoice').text(' - ' + ZoneName);
appP08.controls.divZone.hide();
}
});
});

您找到的解决方案并不正确,因为如果您从开发环境更改为生产环境,那么生成的路由将再次错误。

关于javascript - Symfony 和 Ajax 错误 404,Symfony 工具栏中没有错误消息,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42797607/

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