Tag: ajax

asynchronous:服务器启动后连接到MongoDB

我很想知道关于Node.jsasynchronous和MongoDB的一些事情。 如果服务器在我连接之前启动,并且我的模板或Ajax依赖于来自数据库的数据将服务(我预编译我的句柄)HTML文件失败或将我的模板/ Ajax等待连接,然后继续? 如果它的工作,我想更好地了解它是如何工作的! 如果失败了,我该如何“优雅地修复”呢? 这是一个解决scheme的例子,使用提升(对我来说似乎很糟糕): //connect to db mongodb.connect("someDBplace", (err, db)=>{ if(err){ return generalLogger.error(`The database has failed to connect: ${err}.`); }else{ //start the server now: generalLogger.info(`Connected to database: ${stringify(db.databaseName)}.`); server.listen(`${port}`, (err)=>{ if(err){ generalLogger.error(err); } //for demo: console th eMIMEtype generalLogger.info(`The Server started on port: ${port}.`); }); } });

连接到API与后端呈现的WebApp之间的区别

有时当我创build基本的Web工具时,我将从一个nodeJS后端开始,通常使用ExpressJS创build一个API服务器。 当某些路由命中时,服务器通过使用连接的实时状态呈现来自EJS的HTML来作出响应,然后将其发送到浏览器。 这个应用程序通常会公开一个公共静态资源的目录,并将提供这些资源。 我想这会造成这种forms的Web应用程序的大量开销,但我不确定。 其他时候,我将从一个API开始(这可能是完全相同的nodeJS结构,没有HTML呈现,只是状态pipe理和API的暴露),我会build立一个Angular2或其他HTML网页,将连接到API,加载加载信息,并填充页面中的数据。 这些页面往往依赖于大量的AJAX调用和jQuery,以便在一堆asynchronouscallback触发后刷新angular度组件。 在这个结构中,我将使用像Apache这样的Web服务器来提供所有文件并定义路由,而网页中的JS将完成剩下的工作。 两者的整体优势和劣势是什么? 为什么我应该使用一种策略呢? 他们都是可行的,并依赖于规模和使用? 我想用负载平衡器进行水平缩放可以适用于这两种情况。

NodeJs exec-php模块,php不是一个命令

我想从我的nodejs文件调用一个php文件。 我已经安装并需要exec-php模块,它已经安装好了。 我得到的错误是: { Error: Command failed: php C:\Users\***\Desktop\nodejs\xxchat11\node_modules\exec-php\lib\php\cli.php -pC:\Users\***\AppData\Local\Temp\tmp-129647qzlx6s.gh55ewmi.tmp -rC:\Users\***\AppData\Local\Temp\tmp-12964 7pid95t.8vpiizfr.tmp 'php' is not recognized as an internal or external command, operable program or batch file. at ChildProcess.exithandler (child_process.js:204:12) at emitTwo (events.js:106:13) at ChildProcess.emit (events.js:191:7) at maybeClose (internal/child_process.js:886:16) at Socket.<anonymous> (internal/child_process.js:342:11) at emitOne (events.js:96:13) at Socket.emit (events.js:188:7) at Pipe._handle.close [as _onclose] (net.js:501:12) killed: false, […]

如何从节点js读取ajax请求的数据?

我试图从客户端发送数据到服务器(节点js)。 我正在使用ajax。 客户: $("#test_button").on("click",function(){ //alert("shit"); $.ajax( {url: "http://localhost:4000/ajax_check", async: false, type: "POST", data: "{user:balayya,password:hero}", success: function(result){ alert("hooli"); }}); }); 服务器: var app = require('express')(); var express = require('express'); var http = require('http').Server(app); http.listen(process.env.PORT || 4000, function() { console.log('listening on *:4000'); }); app.use(express.static('publuc')); app.get('/', function(req, res) { console.log("new entry page serving"); res.sendFile(__dirname + '/main.html'); }); app.post('/ajax_check', […]

NodeJS Ajax请求失败

我有一个表单,我试图从客户端提交到Express,但每次我得到一个错误,如: 无法POST / request_method 下面是我正在尝试的代码: 的index.html <form id="wizard-content" method="post"> <label>File</label> <input type="file" name="some" id="rsome"> <label>Value</label> <input type="text" name="valSome" id="perfect"> </form> <button type="submit" id="submit_form">Finish</button> <script type="text/javascript"> jQuery('#submit_form').click(function() { if (jQuery(this).text().toLowerCase() === "finish") { submitForm(); } }); var submitForm = function(){ var formData = { 'perfect' : $('#perfect').val(), 'rsome' : $('#rsome')[0].files[0] }; if(formData){ $.ajax({ url : '/request_method', […]

ReferenceError:在Ajax调用尝试redirect用户之后没有定义窗口

我是新来的EJSexpression。 我试着做一个Ajax的后请求提交给数据库使用的值。 $.ajax({ type: 'POST', url: '/register', data: user, success: function(data){ console.log(data); return data; } }); return false; 这个调用是成功的,然后控制转到调用函数 var sess; app.post('/register',urlencodedParser,function(req,res){ sess=req.session; var newUsr=Users(req.body).save(function(err,data){ if(err) throw err; console.log(" back from ajax with data :::; " + data); console.log("step1 cleared"); sess.registered=true; }); console.log("step2 cleared"); location.window.href = '/index2'; }); 我希望用户在注册成功后能看到索引页,但是我得到了下面的错误。 ReferenceError: location is not defined 我觉得位置对象是不是在节点,因此这个错误,但我不知道如何解决这个问题,请build议

Ajax DELETE请求似乎只删除列表中的第一个项目

我一直在重复使用这些代码进行不同的项目,但是对于这个项目来说,这似乎不起作用。 我正在使用Express和Mongoose,并使用它来存储大学课程详细信息列表。 这是Schema。 exports = module.exports = function(app, mongoose) { var moduleSchema = new mongoose.Schema({ pivot: { type: String, default: '' }, module_name: { type: String, required:true}, module_code: {type: String, required: true}, description: {type: String} }); moduleSchema.plugin(require('./plugins/pagedFind')); moduleSchema.index({ pivot: 1 }); moduleSchema.index({ module_name: 1 }); moduleSchema.index({ module_code: 1 }); moduleSchema.index({ description: 1 }); moduleSchema.index({ search: […]

节点js ajax发布请求

它除了Node js和AJAX POST的错误之外,Node请求不能检查表单数据是否正确处理,总是会掉到错误函数,下面的任何解决scheme都要检查AJAX NODE JS CODE $("#submit_btn").click(function() { $.ajax({ url: '/process_add', type: 'POST', dataType: "json", data: $('#process_form').serializeArray(), success:function(data) { alert('done'); }, error: function(errorThrown,data) { alert('Error'); } }); }); router.post('/process_add', function(req, res, next) { var item = { name: req.body.name, content:req.body.content }; mongo.connect(url, function(err, db) { assert.equal(null, err); db.collection('data_collection').insertOne(item, function(err, result) { assert.equal(null, err); console.log('Process added'); […]

将jQuery ajax调用转换为vanilla Javascript – 不能POST / public / error

所以这是我昨天的一个问题的轻微后续( 更新node.js中的HTMLinput值,而不改变页面 ),我试图提交一个HTML表单,发送一个Ajax请求到我的服务器,提供两个数字,这些号码被添加到服务器上,然后显示在与表单相同的HTML页面上的一个框中。 到目前为止,我收到了JQuery中的一个解决scheme,这个解决scheme非常有用,但是我的经理希望在JavaScript中完成各种原因。 到目前为止,我已经走了一段路,但是很多人为此使用JQuery,而香草javascript方法的例子很less。 目前,在提交页面抛出“不能POST / public /”,我是新来的Ajax,所以我相当确定问题在那里。 我的(工作)JQuery代码: $(document).ready(function(){ $("#add").submit(function(event){ event.preventDefault(); var num1 = $("#numa").val(); var num2 = $("#numb").val(); $.ajax({ method: "post", url: '/add', data: JSON.stringify({ num1: num1, num2: num2 }), contentType: 'application/json', success: function(data){ $("#answer").val(data); } }) }) }) 我尝试转换为javascript: function add(){ var xml = new XMLHttpRequest(); var num1 = document.getElementById("numa"); var […]

Node.js有没有办法在JSON响应中发送数组?

我试图找出是否有一种方法来发送一个数组在一个JSON响应在一个node.js我已经附加dataPoints1,dataPoints2和dataPoints3响应,他们是数组。 但是,从Ajax请求接收时,它只是成为一个string,它是[object object],[object object] responsearray = '{"result":{"System":"Ready","Allcompleted":"completed","dataPoints1":"'+dataPoints1+'","dataPoints2":"'+dataPoints2+'","dataPoints3":"'+dataPoints3+'"}}'; res.setHeader('Content-Type', 'application/json'); res.type('application/json'); res.send(responsearray); 有没有一种方法,我可以从一个Ajax调用发送一个JSONstring中的数组,然后把它回到一个数组? 谢谢