1

i wan't to run a jquery but when i run it i first get my value and then the code failed error. What does this mean? Here you can find my json en ajax file. It seems that the json won't go to my ajax file.

$("#submit_tosolve").on("click",function(e){
     //data uitlezen
    alert("ok");
    var message =$("#bugcommentaar").val();
    console.log(message);
    alert("ok");
    //data naar database sturen

    var request = $.ajax({
      url: "ajax/facebook_ajax.php",
      type: "POST",
      data: {message : message}, //JSON
      dataType: "json"
    });

    request.done(function(msg) {
    if(msg.status=="success"){

      var update='<div style="display:none;" class="span4">'+
      ' <table><tr><td><h5><p>'+ message+'comment posted </p></h5></td></tr></table></div>';


    $("#singleBug table").prepend(update);
    $("#singleBug table tr td").first().slideDown(); //dit gaat werken op elke browser, de eerste eruit halen



      }
    });

    request.fail(function(jqXHR, textStatus) {
      console.log("request failed" +textStatus);
    });


    });

my ajax file :

<?php
    include_once("../classes/Bug.class.php");

        $feedback=array();
        if(isset($_POST['message'])) 
        {
            try
            {
                $Bug = new Bug();
                $Bug->Commentaar=$_POST['message'];
                $Bug->Bug_id=$_SESSION['id2sessioncommentaar'];
                $Bug->UpdateCommentaar();   
                $feedback['text'] = "Your comment has been posted!";
                $feedback['status'] = "success";
            }
            catch(Exception $e)
            {
                $feedback['text'] = $e->getMessage();
                $feedback['status'] = "error";
            }

            header('Content-Type: application/json' );
            echo json_encode($feedback);


        }


?>
Glenda
  • 79
  • 3
  • 3
  • 10

1 Answers1

1

Okay 2 things :

  • prefer using callback methods when playing with ajax
  • always mention your navigator when playing with web :)

    $.ajax({
     url: "ajax/facebook_ajax.php",
      type: "POST",
      data: {message : message}, //JSON
      dataType: "jsonp", //Solution 2: try this type instead, it also automatically sets cache to false
      cache: false, // Solution 1 : prevent IE caching response (basically adds a random argument to the request)
    
    
     success:function(data, textStatus, jqXHR) {
            var update='<div style="display:none;" class="span4">'+
            ' <table><tr><td><h5><p>'+ data+'comment posted </p></h5></td></tr></table></div>';
            $("#singleBug table").prepend(update);
            $("#singleBug table tr td").first().slideDown(); //dit gaat werken op elke browser, de eerste eruit halen
       },
     error:function(jqXHR, textStatus, errorThrown) {
        console.log("request failed" +textStatus);
     }
    });
    

Also, jQuery works with UTF8 by default, your server files need to be written in UTF8 and your request too.

 header('Content-Type: application/json; charset=UTF-8' );

EDIT : btw, after a short googling with "jquery ajax json 304" I found this post jQuery AJAX producing 304 responses when it shouldn't with the exact same problem and exact same solution (cache:false)

Community
  • 1
  • 1
TecHunter
  • 6,091
  • 2
  • 30
  • 47