Tag: node.js

来自express static html文件的$ .getJSON()调用无法获取数据

我使用express模块​​作为我的node.js服务器的基础,并设置一个静态中间件,如下所示: self.app.use(express.static(__dirname)); 在根文件夹中,我有一个html文件,其中包含以下url到远程服务器上的php脚本(与托管node.js应用程序的服务器完全不同),它返回jsonp数据(从dataprovider转换为xml数据): var strURL = ‘http://example.com/jsonp.php?callback=?&url=http://dataprovider.com/1.4/?arg1=xyz;arg2=abc’; 然后jquery getJSON调用实际获取json数据: $.getJSON( strURL, function (jsondata) { // do some stuff with the json data } ); 但是当我从静态node.js文件夹加载正在提供的html文件时,没有返回任何数据……代码永远不会到达jsondata函数。 但是,加载放在“普通”服务器上的非常相同的html文件,数据被提取得很好,而且如果我直接加载strURL,则返回数据OK。 我怀疑这与跨域问题有关,但对于我的生活,我无法使用express在静态node.js服务器中使用该页面。 我已经尝试了各种解决方案,但现在我感到非常困惑和沮丧! 欢迎任何帮助。

NodeJS HTTP请求连接的close事件被触发两次

我正在使用NodeJS中的长轮询实时AJAX应用程序。 我想检测用户何时关闭具有待处理长轮询请求的选项卡。 我使用NodeJS连接关闭事件: request.connection.on(‘close’, function () { console.log(request.url); … }); AJAX循环如下所示: var EventLoop = new (function () { var self = this; this.listen = function () { $.postJSON( ‘recieve-events’, { /* some data not important for this issue */ }, function (data) { … self.listen(); } ); } })(); 这就是postJSON函数的定义方式: $.extend({ postJSON: function (url, data, […]

将数据从jade传输到jquery,反之亦然

我的index.js文件: res.render(‘index’, {data:{‘hello’:’world’}}); 我的玉文件: p #{data} script(src=”/javascripts/app.js”) 这将打印值json对象。 现在在我的app.js文件console.log(data); 给出错误,说明数据未定义。 如何访问我的javascript文件中从index.js文件传递的数据。

为什么所有的进程函数都没有为fileupload插件触发

我使用( blueimp fileupload插件) https://github.com/blueimp/jQuery-File-Upload 以下function都没有触发: $(‘#fileupload’) .bind(‘fileuploadprocessstart’, function (e) {/* … */}) .bind(‘fileuploadprocess’, function (e, data) {/* … */}) .bind(‘fileuploadprocessdone’, function (e, data) {/* … */}) .bind(‘fileuploadprocessfail’, function (e, data) {/* … */}) .bind(‘fileuploadprocessalways’, function (e, data) {/* … */}) .bind(‘fileuploadprocessstop’, function (e) {/* … */}); 我附上了这样的东西: 我已经包含了这些额外的脚本 jquery.fileupload-process.js https://github.com/blueimp/jQuery-File-Upload/blob/master/js/jquery.fileupload-process.js jquery.fileupload-image.js https://github.com/blueimp/jQuery-File-Upload/blob/master/js/jquery.fileupload-image.js $(‘#fileupload’).fileupload({ url: ‘ulr’, […]

将angular.js中的JSON发送到node.js

如果我从前端angular.js发送JSON到backend node.js,它会给我一个错误: TypeError: Cannot read property username of undefined 。 我也用Express。 前端(Angular.js) : var username_data = $scope.LoginUsername; var password_data = $scope.LoginPassword; var data = {username : username_data, password : password_data}; $http.post(‘/api/login’, data) .then(function (res) { //to do… }); 使用Restful API的后端(node.js): app.post(‘/api/login’, function(req, res) { console.log(req.data.username); console.log(req.data.password); });

将默认参数传递给browserify模块

我正在重构一个javascript代码库并正在实现,但我是节点的新手。 我可能遇到这样的代码: foo.js var foo = {}; foo.bar = function(baz) { $(‘body’).append(baz) } 我将重构为以下内容: foo.js var $ = require(‘jquery’)(window); var foo = {}; foo.bar = require(‘./bar’); bar.js module.exports = bar = function(baz) { $(‘body’).append(baz); } 在调用foo.bar(baz)时,将jQuery对象从foo.js传递给bar.js而不干扰baz参数的正确方法是什么?

使用jquery的$ .ajax来获取node.js脚本的输出,而不是它的代码

我正在构建一个小型原型,并存在以下问题: 我试图在客户端jQuery和服务器端node.js之间进行通信; 当我向我的node.js代码发出jQuery ajax请求时,它只是给我代码,而不是代码的输出。 我究竟做错了什么? client.js的一部分: $.ajax({url : ‘./../includes/get_data.js’, success : function(data) { alert(‘success!’); }, error : function(data) { alert(‘error!’); } }); get_data.js: var fs = require(‘fs’); console.log(‘test’); 当我向get_data.js发出请求时,我想要的输出是:test 但相反,我得到了源代码: var fs = require(‘fs’); console.log(‘test’); 非常感谢

如何在Node.js中获取POSTed(jquery)数组数据(使用express)

我正在尝试将数组发布到我的服务器。 但我很难做到这一点。 我试图发布的数组是一个动态结构化的对象数组,因此我不知道它的长度。 更确切地说,我的数组就是这种forms。 var names =[{id:1, name:”nick”},{id:2,name:”bob”},{id:3,name:”john”}…..{id:n, name:”whatever”}] 我使用jquery发布: $.post(“save_names”, { ‘names[]’: names }, function(results) { alert(results); }); 我的节点代码如下:(我使用stormpath-express) app.post(‘/save_names’, config.access_group, function(req, res) { console.log(“body “, req.body); }); 这样我从console.log获得以下内容 body { ‘names[]’: [ ‘[object Object]’, ‘[object Object]’, ‘[object Object]’ ] } 当我尝试打印数组时: console.log(“body “, req.body.names); 我的body undefined 有人可以解释为什么会这样吗? 如何解决我的错误,为什么我不能只发布名称:名称和简单的工作?

从节点js到django的csrf问题

我想将node.js中的csrftoken传递给django。 我的server.js中有这段代码 socket.on(‘unread global’, function (data) { var values=querystring.stringify(); var options = { hostname:’localhost’, port:’8000′, path:’/chat/global_unread/’, method:’POST’, headers:{ ‘Content-Type’:’application/x-www-form-urlencoded’, ‘Cookie’: ‘csrftoken=’ + data.csrf, ‘Content-Length’:values.length } } 我的数据来自一个调用我的server.js的jquery函数 var datos = { // datos is the data that will be send to Node.js user: USER_PK_CHAT, csrf : window.CSRF_TOKEN }; socket.emit(‘unread global’, datos); 但最后我总是在我的控制台中得到同样的错误 失败原因: CSRF token […]

使用npm安装后如何使用节点项目添加jquery?

我已经用npm(npm install jquery –save)安装了jquery,并将jquery添加到我的node_modules文件夹中。 我在html页面上的脚本标记使用什么路径? 这是我的目录结构的样子(仅包含必要的内容) -Project Root -node_modules -jquery -dist -jquery.js -src -index.js -index.html -package.json 对于socket.io,它看起来像这样 我试过这个用于jquery,但它没有用 任何帮助将不胜感激! 谢谢!