I cannot succeed when making a request via ajax

Asked

Viewed 45 times

0

I take in my JS the month the user chose in the HTML select, if in case the value month has been set it executes the function Filter_data_bank as the value, if not I define that the value will be "m", but it does not make the request and is just falling in the error condition. I hoped he’d show me "okay".

Code:

ajax.js

$(function(){
  mostraMesSelect();
  var mes = pegaMesSelect();

  if(typeof mes == "undefined"){
    Filtra_data_Banco("m");
  }else{
    Filtra_data_Banco(mes);
  }
});

function Filtra_data_Banco(valor){
  $.ajax({
    type: "POST",
    url: 'ajax-FiltrosDashboard.php',
    data:{
        mesSelect:valor
    }, 
    dataType: "json",
    success:function(data){
        console.log(data);
    },
    error:function(){
        console.log("Erro indesperado "+ valor);
    }
  });
}

function pegaMesSelect(){
  var mesValue = $("#mesSelect");

  mesValue.change(function(){
    console.log(mesValue.val());

    return mesValue;
  });
}

function mostraMesSelect(){
  $("#mesSelect").change(function(){
    var mesText = $("#mesSelect option:selected").text();

    $("#mesSelecionado").html('<h2 class="text-center">'+mesText+'</h2>');
  });
}

ajax-Filtrosdashboard.php

<?php

if($_POST['mesSelect']){
  echo json_encode($_POST['mesSelect']);
}

Console Output:

inserir a descrição da imagem aqui

  • Change json_encoder to json_encode

  • Even so, without success

  • Try debuggar by browser(Developer Tool), make sure that the console tab displays any errors. You can also view in the network tab if the ajax-Filtrosdashboard.php page was called correctly and which parameters were sent.

1 answer

2


First we need to check which error is being generated in the request.

To URL is correct? The path is correct?

Replace your Ajax function with:

$.ajax({
 type: "POST",
 url: 'ajax-FiltrosDashboard.php',
 data:{ "mesSelect": valor }, 
 dataType: "json",
 success:function(data){
  console.log(data);
 },
    error: function (jqXHR, exception) {
        console.log(jqXHR.status);
    },
});

And as has been said, in the file .php change to:

<?php

if(isset($_POST['mesSelect'])){
    echo json_encode($_POST['mesSelect']);
}

Realize that your request contains: dataType: "json".

According to the documentation of jQuery:

Evaluates the Response as JSON and Returns a Javascript Object. (...) The JSON data is Parsed in a Strict Manner; any malformed JSON is Rejected and a parse error is thrown.

What does this mean? Your request is returning invalid JSON.

Solution: Make sure that the server code (PHP) is returning a valid JSON.

  • 1- The path is correct; 2- Replaces ajax; 3- I switched to json_encode Rest: It returned 200 on the browser console.

  • And php’s answer is which? in network click the request and the in the Answer tab to have access

  • @Alexandrevieiradesouza See my edition.

  • that’s right, thank you

Browser other questions tagged

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