Search code examples
javascriptajaxrequestxmlhttprequest

Unexpected behavior on AJAX query


I do some requests to update database without reloading, but I have some weird behaviors on my requestion.

Here is my code

jQuery("#question1").click(function () {
  jQuery.ajax({
    //L'URL de la requête 

    var nbvues = jQuery("#count1").text();
    alert(nbvues);
    url: "/php/sky-forms-pro/countfaq.php?id_faq=1&nb_vues=" + nbvues,
    //La méthode d'envoi (type de requête)
    method: "GET",
    //Le format de réponse attendu
    dataType: "json",
  })
    //Ce code sera exécuté en cas de succès - La réponse du serveur est passée à done()
    /*On peut par exemple convertir cette réponse en chaine JSON et insérer
    * cette chaine dans un div id="res"*/
  .done(function (response) {
    let data = JSON.stringify(response);
    jQuery("#count1").html(data);
  })

  //On peut afficher les informations relatives à la requête et à l'erreur

});
                            

This show me a weird mistakes, I don't know where I'm wrong:

enter image description here


Solution

  • As you can read in the jquery.ajax documentation:

    Data to be sent to the server. If the HTTP method is one that cannot have an entity body, such as GET, the data is appended to the URL.

    When data is an object, jQuery generates the data string from the object's key/value pairs unless the processData option is set to false. For example, { a: "bc", d: "e,f" } is converted to the string "a=bc&d=e%2Cf"

    jQuery("#question1").click(function () {
        var nbvues = jQuery("#count1").text();
        jQuery.ajax({
            url: "/php/sky-forms-pro/countfaq.php",
            method: "GET",
            data: {
                id_faq: 1,
                nb_vues: nbvues
            },
            dataType: "json"
        }).done(function (response) {
            let data = JSON.stringify(response);
            jQuery("#count1").html(data);
        });
    });