Using AJAX in Laravel

Leave a Reply

Comment as a guest.

  1. Thank you for the helpful article.
    I am having an issue however.

    I call a js file with my ajax from the blade view.
    Then the .post call is being made to my controller and I am pretty sure it gets inside the controller/action. But then my browser console is outputting a 500 error:

    POST http://localhost/admin/categories/new 500 (Internal Server Error) jquery.1.10.2.min.js:6
    send jquery.1.10.2.min.js:6
    x.extend.ajax jquery.1.10.2.min.js:6
    x.(anonymous function) jquery.1.10.2.min.js:6
    (anonymous function) custom_js.js:73
    x.event.dispatch jquery.1.10.2.min.js:5
    v.handle

    I am confused as to where the error is occurring.. I have copied create and make_response methods in my controller and it seems to be crashing at the make_response function call….

    any ideas?

    Thanks!

    1. I really can’t glean anything from what you’ve posted. HTTP 500 is a server error meaning something is crashing in PHP (seems likely). You’ll need to either step through your code to debug what’s breaking (xdebug is an excellent tool to have) or examine your error logs & see if there’s anything helpful in there.

      it seems to be crashing at the make_response function call

      _make_response() that I’ve posted above in example is rather straight forward & is a quick wrapper for Laravel code. So instead of doing this:
      [sourcecode lang=”php”]
      return $this->_make_response( json_encode( $my_array ) );
      [/sourcecode]

      you can also do this:
      [sourcecode lang=”php”]
      return Response::make( json_encode( $my_array ) )->header( ‘Content-Type’, ‘application/json’ );
      [/sourcecode]

      Just make sure that whatever you pass to Response::make() needs to be a string, in this case a JSON encoded string.

    1. You can also just return an array in the controller action & Laravel 4 converts it JSON before sending it to browser but that doesn’t set the content type header to application/json. Seems like Response::json() sets the proper headers, hadn’t seen that one. Thanks. :tup:

  2. Can you help me with a code
    accept data->as date->go to DB -> check if date is added previously -> if added return the entire row -> to the same page where date was accepted -> BUT BUT must have no submit button it should have only ONSELECT DATE OPTION (using jquery)
    ————laravel——–
    thanks in advance

    1. In your jQuery code listen for the change event of your select drop-down like:

      [js]
      jQuery( ‘#date-dropdown’ ).on( ‘change’, function() {
      //fire the form submit event here
      jQuery( ‘#my-form’ ).trigger( ‘submit’ );
      } );
      [/js]

      So anytime your drop-down list (of ID date-dropdown) has a change in value, it will trigger the submission of your form (of ID my-form). If you’re using AJAX for form submission then all you need to do is write code to handle form submit.

  3. Just wanted to point out, that with a little effort, this stuff really works. I realized this is the post that got me actually sending stuff back and forth from the server. I use this technique several times a day now, with several variations for purpose. thanks again!

  4. Hi Im getting “Unsupported operand types ”

    on

    $response – array(
    ‘status’ => ‘success’,
    ‘msg’ => “Registration Failed – $messages”
    );
    return Response::json($response);

    in Laravel4 … any clues?

    1. Ok found the error was a silly typo.. had a “-” instead of “=” next to $response. Now I get and out put of

      {“status”:”success”,”msg”:”Registration Failed”}

      But how to I redirect back to the original form?

  5. Hi,
    I want to know from where you are extracting the token
    on this line
    “_token”: $( this ).find( ‘input[name=_token]’ ).val()

    thanks

    1. Its a hidden input field that Laravel adds automatically with a CSRF protection token when Form::open() is used. That’s what we validate in line 15 of SettingsController. Check the Laravel docs here.

  6. Thank you very much for this wonderful article. It took me 2 days for me playing with my codes trying to figure out the same thing before landed on this page. It helped me a lot.

  7. Hi, just a little tip, you can replace :

    “_token”: $( this ).find( ‘input[name=_token]’ ).val(),
    “setting_name”: $( ‘#setting_name’ ).val(),
    “setting_value”: $( ‘#setting_value’ ).val()

    With this :
    $(this).serialize()

    It’s really usefull when you have a huge form 🙂
    ( Or to get the AJAX working for other forms, with the same code )

    1. Thanks for pointing it out Tom. The JS here is just for illustration purposes to show how to use AJAX in a Laravel app & so I kept it as simple as possible. 🙂

Sliding Sidebar