dtvnbe1428
2014-02-04 22:27
浏览 91

在javascript控制台中从php输出数组

i have a simple ajax script that sends 3 variables to an external php script, it then adds them into an array and sends the array back, and i want it to output it in the javascript console, so that i can check that the variables are being passed back successfully, however when i run the script nothing appears in the console, only that

XHR finished loading: "http://localhost/blank/scripts/ajax/profile_password_submit.php". 

Here is the ajax

$("#pro_content_password").submit(function() {

    var url = "scripts/ajax/profile_password_submit.php"; // the script where you handle the form input.
    var js_array=new Array();
    $.ajax({
           type: "POST",
           url: url,
           data: $("#pro_content_password").serialize(), // serializes the form's elements.
           success: function(data){

                js_array=data;

                console.log(js_array);

             },
             dataType: 'json'
         });

    return false; // avoid to execute the actual submit of the form.
});

Here is the external php script

    session_start();
  include '../../connect.php';

  $user_id = "";
  $user_id = $_SESSION['userId'];
  echo $user_id;
 if(empty($_SESSION['userId'])){

     echo "user id session not set";
     exit;
  }


$old_password = $_POST['pro_content_password_old'];
$new_password = $_POST['pro_content_password_new'];
$new_password1 = $_POST['pro_content_password_verify'];


$password_array = array("old"=>$old_password,"new"=>$new_password, "new1"=>$new_password1);


echo json_encode($password_array);

Any ideas? Also i am using Google Chrome console

  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

3条回答 默认 最新

  • donglu9898 2014-02-06 14:31
    已采纳

    Here is the final working version now, its a lot different to my original but it works a lot better

        <script type="text/javascript">
    function submit_profile_password(){
        //var results = document.getElementById("results");
        var result_error = document.getElementById("pro_content_error_password");
        var old_error = document.getElementById("pro_password_old_comment");
        var new_error = document.getElementById("pro_password_new_comment");
        var new1_error = document.getElementById("pro_password_new1_comment");
    
        var oldPass = document.getElementsByName("pro_content_password_old")[0].value;
        var newPass = document.getElementsByName("pro_content_password_new")[0].value;
        var new1Pass = document.getElementsByName("pro_content_password_verify")[0].value;
        var vars = "oldPass="+oldPass+"&newPass="+newPass+"&new1Pass="+new1Pass;
    
        var hr = new XMLHttpRequest();
        hr.open("POST", "scripts/ajax/profile_password_submit.php", true);
        hr.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
        hr.onreadystatechange = function() {
            if(hr.readyState == 4 && hr.status == 200) {
                var data = JSON.parse(hr.responseText);
                //results.innerHTML = "";
    
                for(var obj in data){
                    if(obj == "old"){
                        old_error.innerHTML = "";
                        old_error.innerHTML = data[obj];
    
                    }else if(obj == "new"){
                        new_error.innerHTML = "";
                        new_error.innerHTML = data[obj];
    
                    }else if(obj == "new1"){
                        new1_error.innerHTML = "";
                        new1_error.innerHTML = data[obj];
    
                    }else if(obj == "result"){
                        result_error.innerHTML = "";
                        result_error.innerHTML = data[obj];
    
                    }
    
                    //alert("Key = "+obj+"value = "+data[obj]+"");
    
                }
            }
        }
        hr.send(vars);
        //results.innerHTML = "requesting...";
        return false; 
    }
    
    
    
    
    
    </script>
    

    Thanks all for the help

    打赏 评论
  • duanchui1279 2014-02-04 22:37

    You need JSON.stringify :

    $("#pro_content_password").submit(function() {
    
        var url = "scripts/ajax/profile_password_submit.php"; // the script where you handle the form input.
        var js_array=new Array();
        $.ajax({
               type: "POST",
               url: url,
               data: $("#pro_content_password").serialize(), // serializes the form's elements.
               success: function(data){
    
                    js_array=JSON.stringify(data);
    
                    console.log(js_array);
    
                 },
                 dataType: 'json'
             });
    
        return false; // avoid to execute the actual submit of the form.
    });
    
    打赏 评论
  • duanlun2827 2014-02-04 23:30

    It looks like you're not outputting a proper JSON object. I don't know for a fact, since you haven't shared what your PHP script is outputting, but I have a feeling that this line in that is what's causing your problem:

    echo $user_id;
    

    You're not just outputting a JSON encoded PHP array, you're also outputting the $user_id variable.

    jQuery's ajax success callback only fires if it receives a properly formatted JSON object, which yours is not. It probably looks something more like this:

    1234{"old": "oldpass", "new": "newpass", "new1": "newpass1"}

    打赏 评论

相关推荐 更多相似问题