ajax is not returning success in Laravel

1

Hello, I'm getting to know this framework now. I'm trying to send ajax via post and see if it returns something, but it did not work, follow the problem below:

 $("#modal-comentario").on("click", function(){
        var get_id = $(this).data("obs-id");
        $.ajax({
                headers: {
                    'X-CSRF-Token': $('input[name="_token"]').val()
                },
                url: "{{ URL::to('lista-contatos/update') }}",
                type: "POST",
                dataType: 'json',
                data: {
                    "id": get_id
                },
                success: function(result){
                    alert(result);
                }
        });
 });

ContactController

public function update(Request $request){
    echo "teste";
}

Route

Route::post('/lista-contatos/update',
       ['as' => 'lista-contatos', 
        'uses' => 'ContatoController@update']);

You are not generating alert(result) when I click the button.

    
asked by anonymous 14.09.2016 / 02:49

1 answer

2

In Laravel it is return within the method, so change echo by return :

public function update(Request $request)
{
    return "teste";
}

If information was actually sent to Controller this will solve.

I ended up finding another problem with your Route thus:

Route::post('/lista-contatos/update',[
        'as' => 'lista-contatos', 
        'uses' => 'ContatoController@update'
]);

remove function(){} , it is only placed when you do not have Controller declared.

Functional example:

Page:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <title>Laravel - Test</title>
    {{ Html::script('js/jquery.v1.11.0.js') }}
</head>
<body>
        {{ Form::open(array('route' => 'test.update', 'role'=>'form', 'id' =>'form1','name' =>'form1' )) }}
            <input type="hidden" id="id" name="id" value="1" />
            <button type="button" id="btnEnviar">Enviar</button>
        {{ Form::close() }}
        <script>
            $("#btnEnviar").on("click", function(){
                var get_id    = $("#id").val();
                var get_token = $('input[name="_token"]').val();
                $.ajax({
                    headers: {
                        'X-CSRF-Token': get_token
                    },
                    url: "{{ URL::to('test/update') }}",
                    type: "POST",
                    dataType: 'json',
                    data: {
                        "id": get_id
                    },
                    success: function(result) {
                        console.log(result); //debug
                        alert(result.id);
                    }
                });
            });
        </script>
</body>
</html>

Route

Route::post('/test/update', array(
       'before' => 'csrf', 
       'as' => 'test.update', 
       'uses' => 'TestController@update')
);

Controller

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use Illuminate\Http\Response;


class TestController extends Controller
{
    public function update(Request $request)
    {
        return response()
             ->json(['id' => (int)$request->get('id')]);
    }
}
    
14.09.2016 / 02:56