我正在做一个小型web项目,所以我认为这是学习Symfony框架的机会。
所以问题是,我正在开发一个简单的游戏,玩家可以在他们的库存中存储一些物品,或者更改他们的坐标,为了动态地这样做,我考虑过使用Ajax,但每次查询没有执行,控制台都会向我显示:
500
内部服务器错误
,而且分支路径函数没有正确映射路由。
这是包含JS代码一部分的html.twig文件(为了测试它是否有效,我直接将它插入到html.twing中,但实际上我希望它在外部JS文件中,因为我有很多交互和功能要实现,但不幸的是,我总是面临同样的问题(
<html lang="fr">
<head>
<meta charset="UTF-8">
<title>{% block title %}LIP{% endblock %}</title>
<meta name="viewport" content="width=device-width,initial-scale=1">
{% block stylesheets %}
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/4.7.0/css/font- awesome.min.css">
<link rel="stylesheet" href="https://unpkg.com/leaflet@1.7.1/dist/leaflet.css"
integrity="sha512-xodZBNTC5n17Xt2atTPuE1HxjVMSvLVW9ocqUKLsCC5CXdbqCmblAshOMAS6/keqq /sMZMZ19scR4PsZChSR7A=="
crossorigin=""/>
<link rel="stylesheet" href="{{asset('css/signInUp.css')}}"/>
<link rel="stylesheet" href="{{asset('css/Homestyle.css')}}"/>
<link rel="stylesheet" href="{{asset('css/Playerstyle.css')}}"/>
{% endblock %}
<link rel = "icon" href = "{{asset("Icons/eiffel64.png")}}" type = "image/x-icon">
{% block javascripts %}
<script src="https://unpkg.com/leaflet@1.7.1/dist/leaflet.js"
integrity="sha512- XQoYMqMTK8LvdxXYG3nZ448hOEQiglfqkJs1NOQV44cWnUrBc8PkAOcXy20w0vlaXaVUearIOBhiXZ5V3ynxwA=="
crossorigin=""></script>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.min.js"></script>
<script>
$(document).ready(function () {
console.log("executing internal ajax!");
//var idMagasin = $('#choixMagasin option:selected').attr('id');
$.ajax({
method: "POST",
url: "{{path('move_player')}}",
//data: {id: idMagasin},
success: function(data){
console.log(data);
//var periode = data.periode;
//console.log(donnees);
}
});
});
</script>
{% endblock %}
</head>
{% block body %}
<body>
<div class="section1" id="mapid">
</div>
<div class="section2" id="playerInterface">
{% if app.user %}
<a href="{{path('logout_Player')}}" id="logout">Déconnexion</a>
{% else %}
{% endif %}
<div move-path="{{path('move_player')}}"></div>
</div>
<div class="footer">
<p>2020 Copyright © ENSG Géomatique Developped by MaghraouiDE & Namekon Teulong PF</p>
<a href="https://www.facebook.com/ENSGeomatique" class="fa fa-facebook"></a>
<a href="https://fr.linkedin.com/edu/ecole-nationale-des-sciences-g%C3%A9ographiques-12351" class="fa fa-linkedin"></a>
<img src="{{asset('Icons/logo_ensg.png')}}" class="logo"/>
</div>{% if app.user is defined %}
{% set player_data = {
pseudo: app.user.Username,
longitude: app.user.longitude,
latitude: app.user.latitude,
inventaire: app.user.inventaire,
} %}
<div data-player='{{ player_data | json_encode | raw }}'>
</div>
{% endif %}
{% block javascript %}
<script src="{{asset('js/code.js')}}"></script>
{% endblock %}
{% endblock %}
</body>
</html>
这是我的控制器:
<?php
namespace AppController;
use SymfonyBundleFrameworkBundleControllerAbstractController;
use SymfonyComponentHttpFoundationResponse;
use SymfonyComponentRoutingAnnotationRoute;
use SymfonyComponentHttpFoundationRequest;
use AppEntityItems;
use AppEntityPlayers;
use AppEntityInventaire;
class InteractionsController extends AbstractController
{
/**
* @Route("/interactions", name="interactions")
*/
public function index(): Response
{
return $this->render('interactions/index.html.twig', [
'controller_name' => 'InteractionsController',
]);
}
/**
* @Route("/interactions/move_player",name="move_player")
*/
public function moveplayer(Request $request,EntityManagerInterface $em){
$player = $this->getUser();
return new JsonResponse(array("username"=>$player->getUsername(),"longitude"=>$player->getLongitude()));
}
}
/**
* @Route("/interactions/affiche_scenario", name="affiche_scenario")
*/
public function afficheScenario(Request $request){
if ($request->isXmlHttpRequest()){
$repository = $this->getDoctrine()->getRepository(Scenarios::class);
return new JsonResponse($repository->findBy($request->request->get("num_scenario")));
}
else{
return new JsonResponse(array["error"=>"Something went wrong!"]);
}
}
}
/**
* @Route("/interactions/add_item",name="add_item")
*/
public function addItem(Request $request,EntityManagerInterface $em){
$player = $this->getUser();
$inventaire=$player->getInventaire();
return new JsonResponse(array("player"=>$player,"inventaire"=>$inventaire->getItem_id()));
}
我从这些控制器中删除了不同的处理,因为我只想测试不起作用的JSON响应,我还应该补充一点,我尝试使用其他方法(在没有jQuery的情况下,在经典JS中使用ajax查询,使用XMLHttpRequest,还获取API,我已经在其他应用程序中使用过这些方法,但我不知道我在Symfony环境中做错了什么。
我面临的这个问题的答案很愚蠢!我只是忘记导入JsonResponse类,这就是错误500的原因:内部服务器错误,我希望这对一些人来说是有用的。感谢所有花时间阅读我的帖子并试图提供帮助的人。