1
I am trying to send a text that is typed via ajax to a php page that will make a query using this text that is received. I want to know how to send the variable value nmCliente
for the php page. I tried the following code and the return was 500 (Internal Server Error)
. I’m using the framework Symfony
Follow the codes.
Jquery
var nmCliente = $("#nome").val();
$.ajax({
url: "../buscacliente",
type: "POST",
data: nmCliente,
dataType: "text"
}).done(function(resposta) {
console.log(resposta);
}).fail(function(jqXHR, textStatus ) {
console.log("Request failed: " + textStatus);
}).always(function() {
console.log("completou");
});
PHP
/**
* @Route("/buscacliente", name="busca_cliente")
* @Method({"GET", "POST"})
*/
public function buscaContratoAction(Request $resquest)
{
if ($request->isXMLHttpRequest()) {
return new JsonResponse(array('data' => 'this is a json response'));
}
return new Response('This is not ajax!', 400);
}
Error 500 is probably by using
../
in the URL. The filesystem is totally different from the URL (although they look similar). See here, here or here (). Anyway, use absolute URL in AJAX. In the links above there are examples.– Gabriel Heming
The URL is correct, it is a pattern that has to be used in this framework. If I change anything the answer is `404 (Not Found)
– Diego Soares
The only mention that is correct is that if you change error 404. But default framework is not. Also
../
is not at all recommended (can check recommended forms here or as the framework uses with Twig here). However, if you are not causing the problem, check the server logs. Error 500 is server error and does not mean anything else.– Gabriel Heming