0

I am trying to use Ajax to reload data from a database. However, the Ajax doesn't call the controller action that I specified in the url:. Here is my Ajax code:

function selectFieldChanged(id){
    $.ajax({
        type: "POST",
        url: Routing.generate('demo_ajax'),
        data: id,
        success: function(){
            alert("Success");
        },
        error: function(XMLHttpRequest, textStatus, errorThrown)
        {
            alert('Error : ' + errorThrown);
        }
    });
}

$(document).ready(function(){
    var id = $(this).val();
    $('#form_patient').change(function(){selectFieldChanged(id)});
});

The routing.xml :

demo_ajax:
    pattern: /ajax/patient
    defaults: { _controller: DemoBundle:Default:index}
    options:
        expose: true

So, I tried to simply echo the value out in the indexAction to see whether it is called or not.

public function indexAction(Request $request)
{   
        if($request->isXmlHttpRequest()){
            echo "xmlHttpRequest is called";
        }
        if($request->getMethod()=='POST'){
            echo 'POST is called';
        }
}

However, I didn't get anything from the indexAction but I got the alert message, `Success, from my Ajax What did I do wrong?

code-jaff
  • 9,230
  • 4
  • 35
  • 56
lvarayut
  • 13,963
  • 17
  • 63
  • 87

1 Answers1

2

The success callback receives data from your server so a variable must be declared to capture it:

success: function(data){ // <-----

    console.log(data);

    alert("Success");

},
MonkeyZeus
  • 20,375
  • 4
  • 36
  • 77
  • Ok. I did as you mentioned in the comment and also printed it out. I can see that the response data is passed from the `indexAction` to the `Ajax` as well. However, I don't quite understand why the `echo` function didn't be compiled. – lvarayut Feb 17 '14 at 15:55
  • You have 2 separate `if()` statements in your PHP so `$request->isXmlHttpRequest()` is returning FALSE and `$request->getMethod()` is not POST. Comment out everything inside `indexAction()` and simply execute this `echo $request->getMethod();` – MonkeyZeus Feb 17 '14 at 16:05
  • I got an alert message - `Error : SyntaxError: Unexpected token P`. – lvarayut Feb 17 '14 at 16:11
  • I have never seen this before `public function indexAction(Request $request)`. Can you explain what it is doing? Specifically the `(Request $request)` portion – MonkeyZeus Feb 17 '14 at 16:16
  • I assume he is using __symfony__ framework, where you can see this kind of syntax. – code-jaff Feb 17 '14 at 16:36
  • `Request $request` is used to get the `Request object` in symfony2. Now, it showed up the `POST` message. I don't know what was the problem. I tried to undo the code to the beginning and it still worked. The cause of problem may occur because of `cache`. – lvarayut Feb 17 '14 at 16:40
  • Thanks @code-jaff! LVarayut: if you are experiencing AJAX cache issues then you can try putting `cache: false,` in your AJAX options. – MonkeyZeus Feb 17 '14 at 16:52
  • But if this is a Symphony cache issue then unfortunately I am not familiar with Symphony – MonkeyZeus Feb 17 '14 at 16:53