Ajax发布到Node.js服务器


                    

我有一个使用express构建的节点应用程序,并且我一直在尝试将ajax发布请求发送到节点服务器。 但是,尽管在这里阅读了许多类似问题的答案,但即使我应该编辑哪些文件,我仍难以解决。 很长时间以来,我一直试图在一个route.js文件中使用http.createServer。 当然,这意味着我无法在与应用程序相同的端口上运行服务器,因为bin-> www文件中已经实例化了服务器。 因此,今天早上,我编辑了bin www文件,以便在向其发送数据时记录该消息。 但是,尽管该功能在我在第二台服务器上进行测试时已经起作用,但是当我将其添加到bin-> www文件中的服务器时,响应不再触发。 实际上,我什至不认为我的Ajax请求已到达服务器。 为了清楚起见,ajax请求是写在script.js中的,它是公用文件夹中的客户端文件,并被发送到在bin-> www文件中实例化的节点服务器。 这是我的代码:</ p>

  *** bin-> www文件***

     var http = require('http');
     var util = require('util')
     var server = http.createServer(app,function(req,res){
         console.log('收到请求:');
         util.log(util.inspect(req))
         util.log('请求已接收:
方法:'+ req。方法+'
url:'+ req.url)
         res.writeHead(200,{'Content-Type':'text / plain'});
         req.on('data',function(chunk){
         console.log('GOT DATA!');
         得到了= chunk.toString(data);
         console.log(获取)
         });
         重发();

    })。listen(3000);
console.log('服务器在端口3000上运行')

*** Script.js->客户端文件***

$ .ajax({
         网址:“ http:// localhost:3000”,
         数据:'{“ abcdefghijklmnopqrstuvwxyz”:“”}',
         输入:“ POST”,
         成功:功能(数据){
             var ret = jQuery.parseJSON(data);
             console.log(ret)
             console.log('成功:')
         },
         错误:功能(xhr,状态,错误){
             console.log('Error:'+ status);
         },
     });
</ code> </ pre>

请帮助! 非常感谢:)</ p>
     </ div>

展开原文

原文

I have a node app built using express and I've been trying for ages to send an ajax post request to the node server. However despite reading many answers to similar problems here I struggled to come to grips with even what files I should be editing. For a long time i was trying to use http.createServer in one of the routes.js files. This of course meant I could not run the server on the same port as the app as there was already a server instantiated in the bin -> www file. So this morning I edited the bin www file so that it would log a message when data was sent to it. However despite this function having worked when I tested it on a second server, the response no longer fires when i added it so the same function to the server in the bin-> www file. In fact I don't even think my ajax request is reaching the server anymore. Just to be clear the ajax request is written in script.js which is a client file in the public folder and is being sent to the node server which is instantiated in the bin -> www file. Here's my code:

***bin -> www file***


    var http = require('http');
    var util = require('util')
    var server = http.createServer(app,function (req, res) {
        console.log('Request received: ');
        util.log(util.inspect(req)) 
        util.log('Request recieved: 
method: ' + req.method + '
url: ' + req.url) 
        res.writeHead(200, { 'Content-Type': 'text/plain' });
        req.on('data', function (chunk) {
        console.log('GOT DATA!');
        got = chunk.toString(data);
        console.log(got)
        });
        res.end();

   }).listen(3000);
console.log('Server running on port 3000')




***Script.js -> Client File***

$.ajax({
        url: 'http://localhost:3000',
        data: '{"abcdefghijklmnopqrstuvwxyz": ""}',
        type: 'POST',
        success: function (data) {
            var ret = jQuery.parseJSON(data);
            console.log(ret)
            console.log('Success: ')
        },
        error: function (xhr, status, error) {
            console.log('Error: ' + status);
        },
    });

Please Help! Thanks so much :)

1个回答


设法得到它。 而不是使用ajax,我只是使用了一个简单的发布请求。 mar shampla:</ p>

script.js(客户端)</ p>

  $。post('/',
         {
             customData:收藏夹
         },
         函数(数据){
             console.log(“已发送消息”);
         });
</ code> </ pre>

和服务器回调(在路由中)</ p>

  router.post('/',function(req,res,next){

  var param = req.param('customData');
  console.log(param +“ =新收到的帖子”);
  console.log(“旧的stormpath =” + req.user.customData.favLoi8ist);
  req.user.customData.favList = param;
  req.user.customData.save();

  newD = param;

  res.send(200,param);
});
</ code> </ pre>
     </ div>

展开原文

原文

managed to get it. instead of using ajax i just used a simple post request. mar shampla:

script.js (client)

$.post('/',
        {
            customData: favourites
        },
        function(data){
            console.log("message sent");
        });

and the server callback(in the route)

    router.post('/', function(req, res, next) {

 var param = req.param('customData');
 console.log(param+" = new received post");
 console.log("old stormpath = "+req.user.customData.favLoi8ist);
 req.user.customData.favList = param;
 req.user.customData.save();

 newD = param;

 res.send(200, param);
});

Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!
立即提问
相关内容推荐