2014-02-12 31 views
0

當我運行alertContents函數時,其他人正在評估,我不希望發生這種情況,我如何註冊一個回調來等待httprequest響應? bascially的onreadystatechange需要使用一個回調的HttpRequest等待響應,與alertcontents功能評估從身體變量集message.js如何使用回調來等待httprequest?

<html> 
<header><title>This is title</title></header> 

<body> 
<script type="text/javascript" src="message.js" ></script> 

<span id="ajaxButton" style="cursor: pointer; text-decoration: underline"> 
Make a request 
</span> 

<script type="text/javascript"> 
(function() { 
var httpRequest; 
document.getElementById("ajaxButton").onclick = function() { makeRequest('message.js'); }; 
function makeRequest(url) { 

    if (window.XMLHttpRequest) { // Mozilla, Safari, ... 
     httpRequest = new XMLHttpRequest(); 
    } else if (window.ActiveXObject) { // IE 
     try { 
     httpRequest = new ActiveXObject("Msxml2.XMLHTTP"); 
     } 
     catch (e) { 
     try { 
      httpRequest = new ActiveXObject("Microsoft.XMLHTTP"); 
     } 
     catch (e) {} 
     } 
    } 
    if (!httpRequest) { 
     alert('Giving up :(Cannot create an XMLHTTP instance'); 
     return false; 
    } 
    httpRequest.onreadystatechange = cbalertContents; 
    httpRequest.open('GET', url); 
    httpRequest.send(); 

    } 

    function cbalertContents(){ 
    alertContents(); 
    } 
    function alertContents() { 
    if (httpRequest.readyState === 4) { 
     if (httpRequest.status === 200) { 
     var resDataString = httpRequest.responseText; 
     var resData = JSON.parse(resDataString); 
     var msg = resData.msg; 
     alert(msg); 
     } else { 
     alert('There was a problem with the request.'); 
     } 
    } 
    } 
})(); 

我的消息的第一個條件,並最終警告「你好JSON」。 js是以下內容,我試圖從json字符串獲取msg屬性

var http = require('http'); 
var responseObject = { msg: 'Hello JSON.' }; 
var responseString = JSON.stringify(responseObject); 
var body = new Buffer(responseString, 'utf-8'); 
exports.handle = function(req, res) { 
    res.writeHead(200, { 
    'Content-Type': 'application/json' 
    }); 
    res.end(body); 
}; 
+0

我不清楚你想說什麼。通過給'httpRequest.onreadystatechange'分配一個函數,你已經有了一個回調函數。什麼不工作?這個腳本目前不受歡迎的結果是什麼? – Butt4cak3

+0

警報提醒('請求存在問題'。);因爲alertcontents中的一個if語句正在評估false,所以我不希望發生這種情況 –

+0

您可以在'alertContents'中爲我們提供httpRequest對象的輸出,例如使用'console.log(httpRequest)'? – Sonata

回答

1

我不認爲回調實現有任何問題。如果您想使用Node.js生成JSON響應,可以嘗試下面的實現。

var http = require('http'); 
var responseObject = {msg:"Hello JSON."}; 
var responseString = JSON.stringify(responseObject); 
var body = new Buffer(responseString, 'utf-8'); 

http.createServer(function (req, res) { 
    res.writeHead(200, {'Content-Type': 'application/json',"Access-Control-Allow-Origin":"*","Access-Control-Allow-Headers":"X-Requested-With"}); 
    res.end(body); 
}).listen(1337, "127.0.0.1"); 
console.log("Node server is running"); 

您可以刪除「訪問控制允許來源」:「*」和「訪問控制 - 允許 - 頭」:從響應頭「X-要求,以」如果沒有跨域關注。

並更新您的ajaxButton點擊處理程序url參數。

document.getElementById("ajaxButton").onclick = function() { makeRequest('http://127.0.0.1:1337/'); }; 

啓動您的節點服務器(節點message.js)和「Hello JSON」消息將按預期顯示。

截圖:

enter image description here

希望這是有幫助的。

+0

這工作正常,但我想用一個主要運行服務器和處理請求我會編輯並把我的main.js。 –