【问题标题】:Control front-end javascript from node.js back-end从 node.js 后端控制前端 javascript
【发布时间】:2015-12-23 16:28:45
【问题描述】:

我正在构建一个聊天应用程序,我想要求用户输入他们的用户名。 JQuery 前端代码将表单滑入视图(准备就绪),将数据存储到变量中,然后加载聊天(当按下回车键或按钮时)。在验证服务器端的用户输入之前,如何停止该动画?我正在使用 node.js 作为后端。有什么想法吗?

提前致谢!

前端 jQuery:

var nameChoice, roomChoice; //to store user input

var initName = function() {

		nameChoice = $("#init-name input").val(); //save chosen name in nameChoice
		$("#current-name").text("Username: " + nameChoice); //put chosen name in chat header
		$("#init-name").animate(
			{"left" : "-35%"}, 300,
			function() {
				$(this).addClass("hidden");
				$("#init-room").removeClass("hidden");
				$("#init-room").animate({"left" : "35%"}, 300);
		});  //remove name form and slide in room form in callback
	} //end initName

var initRoom = function() {
		roomChoice = $("#init-room select").val(); //save chosen room in roomChoice
		$("#current-room").text("Room: " + roomChoice); //put chosen room in chat header
		$("#init-room").animate(
			{"left" : "-35%"}, 300,
			function() {
				$(this).addClass("hidden");
				$("#chat-main").removeClass("hidden");
		}); //remove room form and show page in callback
	} //end initRoom

var btnHover = function() {
	$(".btn-form").hover(
		function() {
			$(this).stop().animate(
			{
				backgroundColor : "#FFBD7A"
			}, 300);
		},
		function() {
			$(this).stop().animate(
			{
				backgroundColor : "white"
			}, 300);
		});
}

var init = function() {

	$("#init-name").removeClass("hidden").animate({"left" : "35%"}, 300); //slide in name form
	
	$(document).keydown(function(event) { //submit choice on enter key
		if (event.which === 13) {
			if (!$("#init-name").hasClass("hidden")) { //if user is choosing name
				event.preventDefault();
				initName(); //call initName function
			}

			if (!$("#init-room").hasClass("hidden")) { //if user is choosing room
				event.preventDefault();
				initRoom(); //call initRoom function
			}
		}
	}); //end enter key submission

	$("#init-name .btn-form").click(initName);
	$("#init-room .btn-form").click(initRoom);

	btnHover();	
} //end init
$(document).ready(init);

我还在学习node,所以还没有后端代码...

【问题讨论】:

  • 请贴一些代码。
  • 但是从一般意义上回答你的问题,当用户按下回车键时,将数据发送到后端(AJAX)并让它返回成功或失败的响应,然后继续如果成功返回,则加载聊天,否则将用户留在表单上。
  • 好的,现在,将用户名发送到服务器的调用在哪里?
  • 它可以在另一个文件中,但此代码需要调用后端要求它验证用户,您可以使用 jQuery Ajax 实现这一点。如果没有后端,那么你就没有真正的聊天应用程序,你只有一个看起来应该存在聊天的页面;因为您需要以某种方式验证用户。
  • 我推荐看看这些:github.com/IgorAntun/node-chatgithub.com/tamaspiros/advanced-chat,和socket.io/get-started/chat,你可以看到他们是如何实现聊天的。这真的不是你的问题,而是下一步。

标签: javascript jquery node.js frontend backend


【解决方案1】:

这个的粗略代码......

$http.post("/login", {"username":username, "password": password}).then(function(response) {

   console.log("success - do animation here");

}.catch(function(response) {

    console.log("failure a non 2xx HTTP response - handle error here");
});

这段代码很粗糙,因为 http 请求应该在服务中,我也没有对这段代码进行 linted,但是你应该得到一般的 IDEA!

道歉,这是有角度的,JQUERY被要求...这里去...

$.ajax({
  method: "POST",
  url: "/login",
  data: { username: username, password: password }
})
.then(
  function( data, textStatus, jqXHR ) {
    console.log("success - do animation here");
  }, 
  function( jqXHR, textStatus, errorThrown ) {
    console.log("failure a non 2xx HTTP response - handle error here");
  }
);

以前从未在 Jquery 中尝试过此方法,但文档建议使用此方法。 检查文档...

http://api.jquery.com/jquery.ajax/

谢谢

编辑:如果基于 promise 的 jQuery 代码不可用:

$.ajax({
  method: "POST",
  url: "/login",
  data: { username: username, password: password }
})
// for older versions of jQuery, replace .done and .fail with .success and .error
.done(function( data, textStatus, jqXHR ) {
    console.log("success - do animation here");
})
.fail(function( jqXHR, textStatus, errorThrown ) {
    console.log("failure a non 2xx HTTP response - handle error here");
}); 

【讨论】:

  • 你在这里假设 Angular 吗?连同可用的承诺?但总体思路适用。能否提供一个 jQuery Ajax 示例?
  • 我很抱歉,我已经习惯于回答 Angular 问题了!我提供了比较 jQuery 解决方案。希望对您有所帮助。
  • 非常感谢您的回答和示例。我还有一个问题:我应该只调用我定义的 jQuery 函数(initName、initRoom 等)而不是 console.log("success - do animation here");,还是不调用?
  • 抱歉延迟回复,圣诞节和所有 :) 是的,用触发动画的代码替换 console.log 语句
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2023-03-12
  • 2018-11-02
  • 1970-01-01
  • 2016-11-09
  • 1970-01-01
  • 2016-01-10
  • 1970-01-01
相关资源
最近更新 更多