ajax json javascript

So in trying to have javascript pass values to a php script using Ajax i get the following error message.

Uncaught SyntaxError: Unexpected end of input

  • Ajax & Session Variables? Worksafe Filter (selective image hiding)
  • Call ajax function with link
  • How can I access multi dimensional JSON data?
  • Clearing balloons google maps: Uncaught ReferenceError: markers is not defined
  • how to post simple ID (jquery ajax) to Action method in asp.net mvc
  • Javascript : get HTML of remote web page
  • In stepping through the code I found saw that my response from my script came back a null string. Would someone please point me to my mistake, I cannot seem to get a grasp on it.

    Here is my javascript (request.js)

    var request;
    
    function getHTTPObject()
    {
        var xhr = false;
        if (window.XMLHttpRequest)
        {
            xhr = new XMLHttpRequest();
        } else if (window.ActiveXObject) {
            try
            {
                xhr = new ActiveXObject("Msxml2.XMLHTTP");
            }
            catch(e)
            {
                try
                {
                    xhr = new ActiveXObject("Microsoft.XMLHTTP");
                }
                catch(e)
                {
                    xhr = false;
                }
            }
        }
        return xhr;
    }
    
    function runAjax(JSONstring)
    {
        // function returns "AJAX" object, depending on web browser
        // this is not native JS function!
        request = getHTTPObject();
        request.onreadystatechange = sendData;
        request.open("GET", "request.php?json="+JSONstring, true);
        request.send(null);
    }
    
    // function is executed when var request state changes
    function sendData()
    {
        // if request object received response
        if(request.readyState == 4)
        {
        // parser.php response
        var JSONtext = request.responseText;
    
        // convert received string to JavaScript object
        var JSONobject = JSON.parse(JSONtext);
    
    
        // notice how variables are used
        var msg = "Number of errors: "+JSONobject.errorsNum+
            "\n- "+JSONobject.error[0]+
            "\n- "+JSONobject.error[1];
    
        alert(msg);
        }
    }
    

    my php file.

    <?php
    //request.php
    $decoded = json_decode($_GET['json']);
    
    $json = array(
        'errorsNum'    =>    2,
    
        'error'       =>    array(
                            "error 1","error 2!"
                     )
    );
    
    $encoded = json_encode($json);
    
    die($encoded);
    
    ?>
    

    and finally my html file where I invoke ajax.

    <html>
    <head>
    <script src="request.js">
    </script>
    </head>
    <body>
    <a href="Javascript:runAjax('vinoth')">call</a><br>
    </body>
    </html>
    

    Thanks in advance.

  • Ajax post request in ios Safari 6 not work
  • Is it possible to upload a file with AJAX and no iframe or Flash?
  • jsTree load children by ajax
  • Jquery ajax data convert to string
  • What happens in JavaScript when an AJAX call returns while the script is executing?
  • Converting html returned from ajax request into an object causes weird complications