php - 使用 WinJS.xhr() 检索 JSON :

标签 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.ini 检索 JSON。但是,当使用 WinJS.xhr 时,我返回的响应始终为空。如果将直接链接放入浏览器中,我将得到如下结果: (包含 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"
        }
    ]
}

最佳答案

toString 是大多数对象的方法。

你的行result.response.toString.length应该是result.response.toString().length

关于php - 使用 WinJS.xhr() 检索 JSON :,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13424198/

相关文章:

php - 我有这个表单数据要发送到数据库中

php - 我想在提交表单后创建一个带有三个按钮的弹出窗口。我怎么做?

javascript - 动态删除 div 时出错

javascript - reactjs popover 组件 - 推送属性/事件处理

javascript - AngularJS 路由是否需要 AJAX 才能执行?

c# - 使用 JObject 即时创建 JSON

php - CKEditor 正在转义 html 元素

javascript - 意外 token : u JSON. parse() 问题

jquery - 使用用户输入更新 JSON 并转换为 XML

php - 根据 GPS 坐标将 Google Map 嵌入 HTML 页面