Javascript对象存在但返回undefined

时间:2014-10-31 17:05:46

标签: javascript php xmlhttprequest undefined

我很难过! 我的PHP回声"结果~~<文件已上传OK> ~~ \ n"。
"导致"被添加为xhrData的键,其值为" File Uploaded OK"。
使用for-in循环记录我期望的数据,但是当我尝试记录xhrData.result时,它返回undefined。 console.log(JSON.stringify(xhrData))也记录了我的期望。我感到很困惑!任何帮助将不胜感激。

xhr.onreadystatechange = function(e) {
    if ( 4 == this.readyState && 200 == this.status ) {
        // PHP returns "result~~<File Uploaded OK>~~\n"
        response = xhr.responseText.split(">~~\n");
        var xhrData = {};
        for(j=0;j<response.length-1;j++) {
            splt = response[j].split("~~<");
            key = splt[0];
            data = splt[1];
            xhrData[key] = data;
        }
        for(key in xhrData) {
            console.log("Key: "+key+"\nData: "+xhrData[key]);
           //Key: result
           //Data: File Uploaded OK
        }
        console.log(xhrData.result);      //undefined
        console.log(xhrData['result']);   //undefined
    }
};

0 个答案:

没有答案