Problem with sending data via ajax and Laravel

Asked

Viewed 56 times

1

Good afternoon, I’m having trouble sending data via ajax, returns the code 200 but returns

Syntaxerror: Unexpected end of JSON input

But I don’t think it’s even enough on the controller

<input type="button" class="btn btn-primary btn-block" onclick="inserirContato()" value="Inserir Contato">
<script>

function inserirContato() {

var cliente = document.getElementById('cliente_id').value;
var nome = document.getElementById('nome').value;
var departamento = document.getElementById('departamento').value;
var email = document.getElementById('email').value;
var telefone = document.getElementById('telefone').value;
var celular = document.getElementById('celular').value;


$.ajax({
    headers: {
            'X-CSRF-Token': $('input[name="_token"]').val()
        },
    type: 'POST',
    dataType: "json",
    url: '{{ route('admin.cadastro.contatos.store') }}',
    data: {
        'contatable_id': cliente,
        'contatable_type' : "App\Cliente",
        'nome' : nome,
        'departamento' : departamento,
        'email' : email,
        'telefone' : telefone,
        'celular' : celular
    },
    enctype: 'multipart/form-data',
    success: function(response) {
        console.log(response);
    },
    error: function(response, textStatus, errorThrown) {
        console.log(response);

    },
});
}
</script>
  • In '{{ route('admin.cadastro.contatos.store') }}' I think it should be "{{ route('admin.cadastro.contatos.store') }}", No? Try switching from single "external" quotes to double...

  • Did not resolve...

  • If you put dataType: 'html', what is returned?

  • Still not returning anything

  • There is an extra comma after function error, Remove it and try again. Just to clarify what I am doing is that this type of error occurs only when the structure of a JSON is not well defined. Somewhere in your code, where you are using a JSON, it is not written correctly...

  • Solved, the route pointed to the wrong controller! Thank you all!

Show 1 more comment
No answers

Browser other questions tagged

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