0

I'm not able to use Ajax from a Blade view with jQuery. As I've researched, it just is as simply as using the function $.get or $.post as usual and taking as first parameter the appropriate root. If I do:

app/views/home/index.blade.php

<script src="//ajax.googleapis.com/ajax/libs/jquery/2.0.0/jquery.min.js"></script>
{{Form::button("d",["id"=>"d"])}}
<script>
    $("#d").on("click",function(){
        $.post("/ajax",function(d){
            console.log("d");
        });
    });
</script>

app/routes.php

Route::post("/",['as'=>'ajax'],function(){
    return 'returned form route';
});

I'm always taking a 500 Internal server error, same case if I try with a get request. Also made:

$.post("{{{route('ajax')}}}",function(){ ...

But also does not work.

I've realized problem is I have an extra http:// at the beginning which shouldn't be there. How could I take appropriate localhost:8000 making it also working on a server?

Also checked Michael Calkins' video How to submit ajax with Laravel video but I think I've done the same and still not working.

If I write as the route the whole URL (localhost:8000/) I take a security error cross origin request and appart from this, it's clear this is not the best solution.

1
  • 500 means a server error occurred. To work out what error, you should have some logs somewhere. Check either your PHP logs, or something in Laravel if it offers that feature. Or, is there perhaps a debug mode in this framework so you can see errors on screen? Commented Apr 27, 2013 at 18:48

1 Answer 1

3

From the details posted it looks like you're doing a GET on a POST route, try changing

Route::post("/",['as'=>'ajax'],function(){

to

Route::post("/ajax", ['as'=>'ajax', function()
{
    // Your code...
}]);

or alternatively change the AJAX from a $.get to a POST

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

4 Comments

Thanks, it was my mistake putting the code here, I've edited it to display how is it, the problem persists. Regards.
Have you tried changing the route to how I have it? Check where the closure sits, your example has the closure outside of the array and from my knowledge it needs to be in the array, so "Route::post("/",['as'=>'ajax'],function(){" turns into "Route::get("/", ['as'=>'ajax', function(){"
Just checked the source code and it's because the method is called incorrectly, to fix it just move the closure into the array like Route::post("/ajax",['as'=>'ajax', function(){
Thanks, that was the problem

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.