weixin_33690963 2015-06-27 12:13 采纳率: 0%
浏览 46

Ajax发布到Node.js服务器

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条回答 默认 最新

  • weixin_33704591 2015-06-27 21:23
    关注

    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);
    });
    
    评论

报告相同问题?

悬赏问题

  • ¥20 matlab计算中误差
  • ¥15 对于相关问题的求解与代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 信号傅里叶变换在matlab上遇到的小问题请求帮助
  • ¥15 保护模式-系统加载-段寄存器
  • ¥15 电脑桌面设定一个区域禁止鼠标操作
  • ¥15 求NPF226060磁芯的详细资料
  • ¥15 使用R语言marginaleffects包进行边际效应图绘制
  • ¥20 usb设备兼容性问题
  • ¥15 错误(10048): “调用exui内部功能”库命令的参数“参数4”不能接受空数据。怎么解决啊