Send an ajax post request to PHP

Asked

Viewed 1,182 times

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.

  • 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)

  • 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.

1 answer

1

Dude, you need to use the Bundle Fosjsrouting in your symfony application. It is highly recommended, and has no secret to use the library! When you have everything up and running, you will use in the parameter inside the Annotation options={"expose"=true}, according to the following example:

/**
 * @Route("/foo/{id}/bar", options={"expose"=true}, name="my_route_to_expose")
 */
  public function indexAction($foo) {
   // ...
  }

And in javascript you will call the route through the function Routing.generate().

          var url = Routing.generate('my_route_to_expose', { id: 10 });
            $.ajax({
                url: url,
                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");
            });

It’ll work fine! Hug!

Browser other questions tagged

You are not signed in. Login or sign up in order to post.