首先我公開一個新手(到這個網站和編碼),所以我提前道歉,如果這個問題的答案過於容易,但我迫切需要一些幫助。添加沒有硬編碼的用戶名,chatbuilder應用程序
我已經開發「chatbuilder」作爲我面試過程中的一部分到開發訓練營。不幸的是,我在包含ajax調用的chatSend函數中對我的用戶名進行了硬編碼。我想使用用戶名的提示輸入,因爲假設這個聊天應該有能力擁有許多用戶,而不僅僅是我和機器人聊天者。我花了太多的時間試圖找出正確的方式來做到這一點,幾個小時的試驗和錯誤,而我意識到修復可能需要大約45秒。我張貼我的完整代碼,如果有人可以告訴我如何正確實施這將不勝感激。謝謝!
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<script src="http://chatbuilder.hackreactor.com/ChatBuilder.js"> </script>
</head>
<body>
<script>
Chat.guide.start();
delete Chat.display;
delete Chat.fetch;
delete Chat.send;
function chatDisplay(str){
$(".messages").append("<li>" + str + "</li>");
};
function chatFetch(fun){
var obj = {};
var result = [];
$.ajax({
type : "GET",
url : "https://api.parse.com/1/classes/chats?order=-createdAt",
dataType: "json",
contentType : "application/json; charset=utf-8",
async : false,
complete : function(data)
{
obj = JSON.parse(data.responseText);
}
});
for (var i = 0; i < obj.results.length; i++) {
result.push(obj.results[i]);
};
result.sort(function(a,b){
a = new Date(a.createdAt);
b = new Date(b.createdAt);
return a<b ? -1 : a>b ? 1 : 0;
});
for (var i = 0; i < result.length; i++){
result[i] = result[i].text;
}
fun(result);
};
function chatSend(str){
var user = document.URL.slice(document.URL.lastIndexOf('=') + 1);
var userpost = $.ajax({
type : "POST",
url : "https://api.parse.com/1/classes/chats",
data: JSON.stringify({text: 'Daniel: ' + str,}),
dataType: "json",
contentType : "application/json; charset=utf-8",
async : false,
});
return userpost;
};
function chatRefresh(){
$(".messages").empty();
function chatTether(arr){
for(var i = 0; i < arr.length; i++){
chatDisplay(arr[i]);
}
};
chatFetch(chatTether);
};
setInterval(chatRefresh,3000);
$(document).ready(function(){
$(".send").on("click", function(){
chatSend($(".draft").val())
$(".draft").val('');
});
});
</script>
<div class="container">
<h2>Fixed Chat</h2>
<input class="draft" type="text"/> <button class="send">send</button>
<ul class="messages">
</ul>
</div>
</body>
</html>
再次感謝您的幫助,非常感謝! – 2014-09-19 22:54:18
只是想出瞭如何接受答案哈哈。抱歉,我說「新手!」 – 2014-09-22 17:34:03