简体   繁体   English

我无法从响应中获取JSON对象到getJSON(jQuery)

[英]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 : 我是JSON的新手,我有php页面(服务器端),其中包含以JSON格式设置的信息,并且我想在客户端(html页面)中获取这些信息,我找到了一个在jQuery中使用此功能'getJSON'的示例一样,但是我想我在使用它时会丢失一些东西,因为我没有得到想要的响应(实际上我什么也没得到),这是php代码:

    <?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 : 我相信我应该提到这是具有xml内容的php页面,我所做的就是将xml格式更改为json格式,这是客户端代码:

    <!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 . 我想接收稍后要解析的JSON对象,我想说的是:我想做一些接近xmlhttprequest并解析响应的操作,但是要使用JSON。 could you please help ?? 能否请你帮忙 ?? I'd really appreciate it . 我非常感激。

The comma behind "filetype" : "Text", makes your json invalid. "filetype" : "Text",后面的逗号会使您的json无效。 Remove that and you should get your json parsed. 删除它,您应该解析您的json。 You can use jsonlint to verify that your json is correct. 您可以使用jsonlint来验证您的json是否正确。

i also noticed that your not using the key that is in your json output. 我还注意到您没有使用json输出中的key

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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