ajax is not returning success in Laravel

Asked

Viewed 305 times

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 didn’t 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);
                }
        });
 });

Contactocontroller

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

Route

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

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

1 answer

2


In Laravel is return within the method, then change echo for return:

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

If the information was actually sent to the Controller this will solve.

I found another problem with you Route leave it so:

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

withdraw the function(){}, is only placed when you don’t have one Controller avowed.

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)

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')]);
    }
}
  • I’ve made these modifications, and it’s still not coming up. I did a test here changing the post route to get and I downloaded the url to see if the string is being printed and it was printable. Weird.

  • @There may be several things, Route equals, it may be wrong setting. If you have debugged the browser, press F12 on the console and run it to give you an idea. Another thing after this line var get_id = $(this).data("obs-id"); give a console.log(get_id). has several problems in your code one of these may contain problems!

  • @I’ve made a functional, simple example that will help you with your solution. It’s very similar, I hope you make the most of it. If you can take this code and click on a separate controller for testing.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.