1

I am using Laravel 5.3 for my application. On a form, I am trying to make ajax call from javascript.

The jquery docs show how to give a static url for the ajax call.

$.ajax({
  url: "test.html",
  context: document.body
}).done(function() {
  $( this ).addClass( "done" );
});

I know I can substitute the test.html with any URI from my routes.php file. But I don't want it to be static. It would be better if I can call a Controller function. This will allow me to change the URI in future without changing the code.

In short, I need a way to call the action() function of Laravel in the javascript. How can I achieve this?

1 Answer 1

1

It's simple.

Just do this.

$.ajax({
  url: {{ action('ControllerClass@functionName', [parameter_list]) }},
  context: document.body
}).done(function() {
  $( this ).addClass( "done" );
});

Let the blade engine handle the rest of the task.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.