简体   繁体   中英

I can't get JSON object from response to getJSON ( jQuery )

I'm new to JSON , I have php page (server side) which include information formatted in JSON and I want to get these information in client side ( html page ) , I found an example using this function 'getJSON' in jQuery to do the same , but I think I'm missing something when using it because I'm not getting the response I want (Actually I'm getting nothing) this is the php code :

    <?php
    //header('Content-Type: text/xml');
    header('Content-type: application/json');
    ?>

    {
        "file": 
        {
            "filename" : "Test.txt",
            "fileCreatingDate" : "15-7-2013",
            "fileModifyDate" : "20-8-2013",
            "filesize" : "3002345",
            "filetype" : "Text",
        }
    }

I believe I should mention that this was php page with xml content and what I did is changing the xml format to json format , and here is the client side code :

    <!DOCTYPE html>
              <html>
              <head>
                  <script src="http://code.jquery.com/jquery-1.9.1.min.js"></script>
                  <script src="http://code.jquery.com/jquery-migrate-1.1.0.min.js"></script>
              </head>

              <body>


              <div id="response">
                  <p id="responseParagraph">Base text</p>
              </div>

              <script>
              //172.25.10.99 is the server ip
                  $.getJSON('http://172.25.10.99/list2.php',
                      function(data) {
                          $('#responseParagraph').append("<p>"+data.responseMessage+"           </p>");
                      });
              </script>
              </body>
              </html>

I would like to receive JSON object to be parsed later , what I'm trying to say is : I want do something close to xmlhttprequest and parsing the response but in JSON instead . could you please help ?? I'd really appreciate it .

The comma behind "filetype" : "Text", makes your json invalid. Remove that and you should get your json parsed. You can use jsonlint to verify that your json is correct.

i also noticed that your not using the key that is in your json output.

 $.getJSON('http://172.25.10.99/list2.php',
    function(data) {
        $('#responseParagraph').append("<p>"+data.file.filename+"</p>");
    });

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM