2014-06-09 65 views
0

新信息開始 我能夠通過更改「現有」代碼中的一行(即我借用的代碼) ,用responseText代替responseXML。這固定了一個關鍵問題,因爲變量details現在包含所需的數據。在downloadUrl中,回調函數參數的值是什麼

我還注意到,當看着包含util.js的「工作示例」鏈接時,正在使用一個附加功能xmlParse(str)。我沒有看到或理解如何使用xmlParse(str),但這可能是我對JavaScript的一種天真。

function xmlParse(str) { 
    if (typeof ActiveXObject != 'undefined' && typeof GetObject != 'undefined') { 
    var doc = new ActiveXObject('Microsoft.XMLDOM'); 
    doc.loadXML(str); 
    return doc; 
    } 

那麼新版本的Google Maps(V3)在使用XML的時候還沒有過,或者說什麼? 無論如何,我可能需要打開一個關於我已經在main.js中遇到的下一個錯誤的新問題,無論這是什麼。

新信息結束

一個完全重寫開始

這第一個腳本可用here。我添加了一些console.log()結果以顯示證實信息包含在request.responseText中的中間結果。

/** 
* Returns an XMLHttp instance to use for asynchronous 
* downloading. This method will never throw an exception, but will 
* return NULL if the browser does not support XmlHttp for any reason. 
* @return {XMLHttpRequest|Null} 
*/ 
function createXmlHttpRequest() { 
try { 
    if (typeof ActiveXObject != 'undefined') { 
    return new ActiveXObject('Microsoft.XMLHTTP'); 
    } else if (window["XMLHttpRequest"]) { 
    return new XMLHttpRequest(); 
    } 
} catch (e) { 
    changeStatus(e); 
} 
return null; 
}; 

/** 
* This functions wraps XMLHttpRequest open/send function. 
* It lets you specify a URL and will call the callback if 
* it gets a status code of 200. 
* @param {String} url The URL to retrieve 
* @param {Function} callback The function to call once retrieved. 
*/ 
function downloadUrl(url, callback) { 
var status = -1; 
var request = createXmlHttpRequest(); 
if (!request) { 
    return false; 
} 

request.onreadystatechange = function() { 
    if (request.readyState == 4) { 
    try { 
     status = request.status; 
    } catch (e) { 
     // Usually indicates request timed out in FF. 
    } 
    if (status == 200) { 
    console.log(request.responseText); 
    console.log(request.responseXML); 
    console.log(request.status); 
     callback(request.responseXML, request.status); 
     request.onreadystatechange = function() {}; 
    } 
    } 
} 
request.open('GET', url, true); 
try { 
    request.send(null); 
} catch (e) { 
    changeStatus(e); 
} 
}; 

我的腳本的關鍵部分是如下,問題是details始終是「空」這可以從日誌下面進一步的讀數可以看到。但我也可以從console.log(request.responseText)看到我需要的數據真的可用。

downloadUrl("details.txt?place=Playground",function(details) { 
    console.log('details: '+details); 
    if (details) { 
     var lines = details.split("\n"); 
     } 
    } 

以下是控制檯日誌。第一行是我希望在變量details中捕獲的信息,它的值爲request.responseText,因爲它代表地圖引腳。

我該如何修改我的代碼,甚至是現有的代碼,以便在現有代碼中連接request.responseText的變量details

ahdkZXZ-c2ltcGxpZnljb25uZWN0aW9uc3IlCxIFR3JvdXAiClBsYXlncm91bmQMCxIDUGluGICAgICAgIAKDA Thu 06 12 2014 1444 29.1109258712 -81.4114379883 Dad 0  0 
util.js:42 
null util.js:43 
200 util.js:44 
details: null ?place=Playground:114 

downloadUrl()is here工作的例子。 (我大概瞭解如何去適應工作示例混亂的部分原因是它使用XML,它不參與我的使用。)

一個完全重寫結束

+1

您需要_pass_該功能,而不是調用它。 – SLaks

+0

你能說更多嗎?我不知道區別。 – zerowords

+1

'downloadUrl(url,readData())'調用函數讀取數據,使用它返回的值作爲數據從服務器到達時執行的函數指針。 'downloadUrl(url,readData);'當數據從服務器到達時執行readData函數,並傳遞適當的參數。 – geocodezip

回答

0

簡短的回答的問題是,實用程序util.js內的回調函數需要一個新的參數 - request.responseText - 未提供且未提及。無意中,我正在關注腳本中的調用函數的參數,而不是實用程序中的參數。

我也被調用函數的第二個參數是回調函數這一事實弄糊塗了,但在回調函數本身中,它是第一個需要調整的參數。傻我。

相關問題