2013-07-25 41 views
3

我嘗試發送回chrome瀏覽器200 OK響應,並且瀏覽器沒有按照我的預期做出反應,他清理屏幕而不是更改特定的'div',這是我的代碼:NodeJS-發送簡單回覆

我的服務器:(node.js的):

function postHandler(request, response) { 
    request.on('data', function(data) { 
     body += data; 
     var parseBody = queryString.parse(body); 

     response.writeHead(200, {"Content-Type": "text/plain"}); 
       response.end("<div>divi</div>"); 
    }); 
    response.on('end', function() { 
     console.log("End_Body: " + body); 
    }); 
} 

和我的JavaScript的瀏覽器的AJAX調用的樣子:

$(document).ready(function() { 
$("#register_form").ketchup(); 

var request; 
$("#submit_btn") 
    .on('click',function() { 

     var username = $('#reg_username').val(); 
     var password = $('#reg_password').val(); 
     var firstname = $('#firstname').val(); 
     var lastname = $('#lastname').val(); 
     var age = $('#age').val(); 

     request = $.ajax({ url: "/welcome.html", 
      type: "POST", 

      data: { 
       'username': username, 
       'password': password, 
       'firstname': firstname, 
       'lastname': lastname, 
       'age': age 
      }}); 

     request.done(function (response, textStatus, jqXHR){ 
      $("#server_answer").text("success"); 
     }); 
     request.fail(function (jqXHR, textStatus, errorThrown){ 
      $("#server_answer").text("fail"); 


      }); 

     }); 
}); 

什麼我做錯了???

請節省我一些時間:) 先進的感謝!

+0

您確保服務器執行呢?嘗試將一些日誌放在request.on('data',function(data){,我沒有在這裏執行代碼,因爲我沒有配置環境,但嘗試調試以查看它通過的位置。 –

+0

did you啓動服務器,並在什麼端口?你有apache服務的原始頁面? – VeXii

+0

本地主機:8080,沒有apache – gran33

回答

1

我沒有執行你的代碼,但我的猜測將是你的頁面刷新時你的點擊處理器結束。

嘗試在點擊處理程序的末尾添加return false;

0

您可以測試以下更改。在服務器:在瀏覽器

request.done(function(msg) { 
    $("#server_answer").text("success"); 
}); 

request.fail(function(jqXHR, textStatus) { 
    $("#server_answer").text("fail"); 
}); 

您爲done參數和fail是在Ajax調用給出的successerror處理

function postHandler(request, response) { 
    request.on('data', function(data) { 
     body += data; 
    }); 
    request.on('end', function() { 
     var parseBody = queryString.parse(body); 

     response.writeHead(200, {"Content-Type": "text/plain"}); 
       response.end("<div>div</div>"); 
     console.log("End_Body: " + body); 
    }); 
} 

+0

從這裏採取:http:// stackoverflow。com/questions/5004233/jquery-ajax-post-example – gran33

+0

是的,他們是正確的,你可以檢查響應是否到達瀏覽器或不。查找問題是否在節點或瀏覽器上。 – user568109

1

儘量不要發送'數據'上的響應,而是從'結束'... 發送響應,請嘗試使用成功方法。

請嘗試以下...在服務器:

function postHandler(request, response) { 
var body = ''; 
//when the request comes with some body 
request.on('data', function (data) { 
     body += data; 
     // 1e6 === 1 * Math.pow(10, 6) === 1 * 1000000 ~~~ 1MB 
     if (body.length > 1e6) { 
      // FLOOD ATTACK OR FAULTY CLIENT, NUKE REQUEST 
      request.connection.destroy(); 
     } 
    }); 

//when the request is finished. (maybe it's data is long and its takes time) 
request.on('end', function() { 
     console.log("End_Body: " + body); 
     response.writeHead(200, { 'Content-Type': 'text/plain'}); 
     response.write('<div>divi</div>'); 
     response.end(); 
    });//end of 'end' event 
} 

請注意,在這裏你actuly忽略了身體(我試圖保持接近你的代碼,因爲我可以)...

在客戶端:

$(document).ready(function() { 
    $("#register_form").ketchup(); 

    $("#submit_btn").on('click',function() { 
     var username = $('#reg_username').val(); 
     var password = $('#reg_password').val(); 
     var firstname = $('#firstname').val(); 
     var lastname = $('#lastname').val(); 
     var age = $('#age').val(); 

     $.ajax({ url: "", 
       type: "POST", 
       data: {'username': username, 
         'password': password, 
         'firstname': firstname, 
         'lastname': lastname, 
         'age': age}, 
       success: function(data) {//on success 
         $("#server_answer").text("success"); 
       }, 
       fail: function (jqXHR,textStatus,errorThrown){ // when the ajax call fails 
         $("#server_answer").text("fail"); 

       } 
     }); //of ajax 
    });//of on 'click' 
}); 

請注意,我刪除的網址(把「」代替),所以它的默認值是本地主機...你所採取的方式,你actully發送POST請求「/welcome.html」但那不是y的地方OU處理這些請求(可能在server.js什麼 - 這是你的本地主機)

我使用了一些this

1

由於瑣Yahav提到的,請嘗試以下操作:

statusCode: { 
200: function() { 
alert('whatever'); 
}}, 

,不要忘了..

return false