使用WinJS.xhr()检索JSON:

时间:2012-11-16 20:48:40

标签: php javascript json windows-runtime

我在winjs.xhr中检索聊天消息时遇到了问题:

  function getMessage() {
        var time = MESSAGE_RETURNED.unixtime;
        if (time == 0) {
            time= window.parent.SESSION.unixtime;
        }

        WinJS.xhr({
            url: "http://www.example.com/scripts/default.php?"
               + "action="+ACTIONS.GET_MESSAGE
               + "&username=" + window.parent.SESSION.username
               + "&session_id=" + window.parent.SESSION.session_id
               + "&unixtime=" + time
        }).done(
           function fulfilled(result) {
               console.log("action=" + ACTIONS.GET_MESSAGE
               + "&username=" + window.parent.SESSION.username
               + "&session_id=" + window.parent.SESSION.session_id
               + "&unixtime=" + time);//CHECK INPUTS , ALL GREEN

               if (result.status === 200) {

                   if (result.response.toString.length === 0) {
                       return false;
                   }//ALWAYS TRUE;
                   else {
                       console.log(JSON.parse(result.response));

                  }
               } else {
                   document.getElementById("messagelogarea").value += result.status + "\r\n";
               }
           });
    }

我正在尝试从php中检索JSON。但是,使用WinJS.xhr时,我给出的响应始终为null。如果将直接链接放在我的浏览器中,我将得到如下结果: (包含3条聊天消息的数组)

{
    "message": [
        {
            "_id": "428",
            "from_TBonlineusers_id": "00477",
            "fromTBonlineusers_username": "GUEST_477",
            "fromTBonlineusers_type": "GUEST",
            "messagetype": "PUBLIC",
            "message": "wat?xxwiii",
            "chatmessagetime": "2012-11-16 15:36:06",
            "unixtime": "1353098166"
        },
        {
            "_id": "429",
            "from_TBonlineusers_id": "00477",
            "fromTBonlineusers_username": "GUEST_477",
            "fromTBonlineusers_type": "GUEST",
            "messagetype": "PUBLIC",
            "message": "wat?xxwiii",
            "chatmessagetime": "2012-11-16 15:42:42",
            "unixtime": "1353098562"
        },
        {
            "_id": "430",
            "from_TBonlineusers_id": "00477",
            "fromTBonlineusers_username": "GUEST_477",
            "fromTBonlineusers_type": "GUEST",
            "messagetype": "PUBLIC",
            "message": "wat?xxwiiir",
            "chatmessagetime": "2012-11-16 15:42:48",
            "unixtime": "1353098568"
        }
    ]
}

1 个答案:

答案 0 :(得分:0)

toString是大多数对象的方法。

您的第result.response.toString.length行应为result.response.toString().length