将数据从客户端传递到服务器

将数据从客户端传递到服务器

本文介绍了Meteor 将数据从客户端传递到服务器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个注册表单,当用户点击提交按钮时,每个文本框中的值将被发送到服务器以插入该数据,并返回真/假.

I have a registration form, and when the user clicks the submit button the value in every textbox will be sent to server to insert that data, and return true/false.

客户:

Template.cust_register.events({
    'click button': function(){
          var email = $('#tbxCustEmail').val();
          var msg = $('#tbxCustMsg').val();
          var isSuccess = insertMsg(email,msg);
          if(isSuccess){
             alert("Success");
          }else alert("Try again");
    }
});

服务器:

function insertMsg(email,msg){
     Messages.insert({Email:email,Message:msg});
     return true;
}

结果证明这是行不通的.如何解决这个问题?很多人说使用发布/订阅",但我不明白如何使用.

This turned out to not work.How to solve this?Many people said "use publish/subscribe", but I don't understand how to use that.

推荐答案

首先,观看介绍性截屏并阅读文档的数据和安全部分.

First, watch the introductory screencast and read the Data and security section of the docs.

您在发布/订阅模型中的代码如下所示:

Your code in a publish/subscribe model would look like this:

常见:

Messages = new Meteor.Collection('messages');

客户:

Meteor.subscribe("messages");

Template.cust_register.events({
    'click button': function(){
          var email = $('#tbxCustEmail').val();
          var msg = $('#tbxCustMsg').val();
          Messages.insert({Email:email,Message:msg});
    }
});

服务器:

Meteor.publish("messages", function() {
    return Messages.find();
});

这篇关于Meteor 将数据从客户端传递到服务器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-05 08:59