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

报告相同问题?

悬赏问题

  • ¥15 聚类分析或者python进行数据分析
  • ¥15 逻辑谓词和消解原理的运用
  • ¥15 三菱伺服电机按启动按钮有使能但不动作
  • ¥15 js,页面2返回页面1时定位进入的设备
  • ¥50 导入文件到网吧的电脑并且在重启之后不会被恢复
  • ¥15 (希望可以解决问题)ma和mb文件无法正常打开,打开后是空白,但是有正常内存占用,但可以在打开Maya应用程序后打开场景ma和mb格式。
  • ¥20 ML307A在使用AT命令连接EMQX平台的MQTT时被拒绝
  • ¥20 腾讯企业邮箱邮件可以恢复么
  • ¥15 有人知道怎么将自己的迁移策略布到edgecloudsim上使用吗?
  • ¥15 错误 LNK2001 无法解析的外部符号