Tag: xmlhttprequest

如何通过XMLHttpRequest创build并保存http长连接

让我说:我想创build一个http长连接。 // nodejs代码 router.get('/random', function (req, res, next) { res.writeHead(200, { "Content-Type": "text/event-stream", "Cache-Control": "no-cache", "Connection": "keep-alive" }); res.write("blabla\n"); }); // js代码 var xhr = new XMLHttpRequest(); xhr.onreadystatechange = function () { if (xhr.readyState == 4) { console.log(xhr.responseText); } if (xhr.readyState == 3) { console.log('receive part of data' + xhr.responseText); setTimeout(function () { // ??????? […]

如何在Node.js中使用XHR API?

这是我以前的问题的后续行动 假设我有一些javascript代码,在客户端(在浏览器中)运行良好。 这段代码使用浏览器API进行了大量的XHR调用。 现在我想在node.js运行这个代码。 node.js是否提供了浏览器中的XHR API?

通过XHR从客户端发送blob数据到NodeJS服务器

我想从JA客户端发送一个blob数据到NodeJS服务器。 现在我有这个客户端代码:[…] recorder.exportWAV(function(blob){audioObj = blob;}); var formData = new FormData(); formData.append("operation", "addMessage"); formData.append("msg", audioObj); var xhr = new XMLHttpRequest(); xhr.open("POST", "http://localhost:8080/sendRecord", true); xhr.onreadystatechange = function(){ if(xhr.readyState==4 && xhr.status==200){ // get response from server } } xhr.send(formData); 我使用一些JS库来logging来自集成麦克风的audio,最后我有一个audio对象是一个blob对象。 而这个si node.js服务器,我想调整接收数据。 var http = require('http'), url = require('url'); var app = http.createServer(function(request, response){ var path = url.parse(request.url).pathname; […]

使用XMLHttpRequest进行内存高效的消息块处理

我有一个XMLHttpRequest与一个progress事件处理程序,请求一个分块的页面,不断发送添加消息块。 如果我没有设置responseType ,我可以在每个progress事件中访问XMLHttpRequest的response属性,并处理额外的消息块。 这种方法的问题在于浏览器必须将整个响应保存在内存中,并且由于这种内存浪费,浏览器最终会崩溃。 所以,我尝试了一个arraybuffer的responseType ,希望我可以分割缓冲区,以防止以前的内存浪费。 不幸的是, progress事件处理程序在这一点上不再能够读取XMLHttpRequest的response属性。 progress事件的事件参数也不包含缓冲区。 这是一个简短的,自包含的例子,我尝试了这个(这是为node.js写的): var http = require('http'); // — The server. http.createServer(function(req, res) { if (req.url === '/stream') return serverStream(res); serverMain(res); }).listen(3000); // — The server functions to send a HTML page with the client code, or a stream. function serverMain(res) { res.writeHead(200, {'Content-Type': 'text/html'}); res.write('<html><body>Hello World</body><script>'); res.end(client.toString() […]

XMLHttpRequest分块响应,只读最后一个响应

我将一个NodeJS应用程序的分块数据发送回浏览器。 这些块真的是jsonstring。 我遇到的问题是,每次调用onprogress函数时,都会添加一个完整数据的string。 意味着响应块号码2被附加到响应块号码1,依此类推。 我只想得到“刚才”收到的大块。 代码如下: console.log("Start scan…"); var xhr = new XMLHttpRequest(); xhr.responseType = "text"; xhr.open("GET", "/servers/scan", true); xhr.onprogress = function () { console.log("PROGRESS:", xhr.responseText); } xhr.send(); 所以实际上,当第三个响应到来时,xhr.responseText的内容也包含第一个和第二个响应的响应文本。 我已经检查过服务器发送了什么,而且看起来没有问题。 使用带有Express的节点,并使用res.send("…")发送几次。 还标题设置如下所示: res.setHeader('Transfer-Encoding', 'chunked'); res.setHeader('X-Content-Type-Options', 'nosniff'); res.set('Content-Type', 'text/json');

Axios与node.js – ReferenceError:XMLHttpRequest未定义

我从一个简单configuration的axios实例触发一个GET请求: var Axios = axios.create({ baseURL: myBaseUrl, headers: {'content-type': 'application/json'} }); Axios.get(url) 并得到以下错误: ReferenceError: XMLHttpRequest is not defined at dispatchXhrRequest (C:\…\node_modules\axios\dist\axios.js:804:24) at xhrAdapter (C:\…\node_modules\axios\dist\axios.js:796:11) at process._tickCallback (internal/process/next_tick.js:103:7) at Function.Module.runMain (module.js:577:11) at startup (node.js:160:18) at node.js:456:3 据我的理解(并根据文档 ),axios应该在web浏览器和node.js上工作。 我错过了什么? 版本: node – 6.2.1 axios – 0.13.1

我如何使用客户端JavaScript从MongoDB和没有框架的Node.js服务器中查找和返回数据?

我已经阅读了所有我能find的问题。 他们都使用快递,mongoose或他们留下了一些东西。 我明白,Node.js是服务器。 我知道MongoDB的require是Node.js服务器用来打开MongoDB连接的驱动程序。 然后,在服务器上,我可以做(从文档 ): var MongoClient = require('mongodb').MongoClient; var assert = require('assert'); var ObjectId = require('mongodb').ObjectID; var url = 'mongodb://localhost:27017/test'; var findRestaurants = function(db, callback) { var cursor =db.collection('restaurants').find( ); cursor.each(function(err, doc) { assert.equal(err, null); if (doc != null) { console.dir(doc); } else { callback(); } }); }; // Connect to the db […]

Node.js应用程序收到GET请求到API的空响应

我是node.js的新手,所以我会尽我所能在这里解释这个问题。 让我知道是否需要任何clerification。 在我的node.js应用程序中,我试图将代码(从第一次调用API的响应接收到的代码)使用该代码向另一个API服务发出第二个请求(GET请求)。 第一个调用的callbackurl是/pass 。 然而,我收到了这个第二个电话的服务的empty response 。 我的理解是,在第一次调用callback之后, app.get('/pass', function (req, res)..被调用并发送一个GET请求。提前致谢! 下面是我尝试从node.js服务器发出GET请求的部分,并收到一个空的响应: app.get('/pass', function (req, res){ var options = { url: 'https://the url that I make GET request to', method: 'GET', headers: { 'authorization_code': code, 'Customer-Id':'someID', 'Customer-Secret':'somePassword' } }; request(options, function(err, res, body) { console.log(res); }); });

NodeJSstream暂停/恢复不适用于XMLHttpRequest,但与curl?

这是一个相当深奥的问题,我不能提供一个小的testing用例,所以提前抱歉。 但是也许有人遇到过这样的事情。 我有这样的代码(使用restify): server.put("/whatever", function (serverRequest, serverResponse, next) { serverRequest.pause(); serverRequest.on("data", function (chunk) { console.log("from outside", chunk); }); doSomeAsyncStuff(function (err) { serverRequest.on("data", function (chunk) { console.log("from inside", chunk); }); serverRequest.on("end", function () { next(); }); serverRequest.resume(); }); }); 当我用CURL打这个服务器时,这个工作很好。 但是当我用XMLHttpRequest命中它的时候,我"from inside"日志"from inside"看到的内容less于"from outside"日志行。 尽pipe我尽最大的努力暂停,但看起来数据事件之一正在迷失。 这是我使用的CURL命令: curl -X PUT -T file.pdf http://localhost:7070/whatever -v 这里是XMLHttpRequest代码(适用于最新版本的Chrome): var arrayBuffer […]

Socket.IO RedisStore和xhr-polling

我有一个问题扩展我的Node.js应用程序在heroku上。 我正在使用RedisStore来保持我的套接字连接在节点进程之间持久。 我正在缩放到2,3个进程。 我在这里https://gist.github.com/1391583跟着这个代码。 在一个进程中一切正常,但是当我扩展时,广播不会到达其他节点。 连接信息是持久的,但是我没有得到持续的握手错误和断开连接。 我的理解是,socket.io RedisStore还可以在内部处理redis Pub / Sub,以保持多个进程同步。 正如这里提到https://groups.google.com/forum/?fromgroups&hl=en#!topic/socket_io/gymBTlruYxs顺便说一句,我申请#848 socket.io补丁,似乎并没有解决问题我。 截至2012年5月14日,Heroku仅支持socket.io的xhr-polling传输,这正是我所使用的。 那么只有长时间轮询的问题呢? 有没有解决这个问题? 提前致谢。