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 metadata提取的PDF元数据,如何转换为一个Excel
  • ¥15 关于arduino编程toCharArray()函数的使用
  • ¥100 vc++混合CEF采用CLR方式编译报错
  • ¥15 coze 的插件输入飞书多维表格 app_token 后一直显示错误,如何解决?
  • ¥15 vite+vue3+plyr播放本地public文件夹下视频无法加载
  • ¥15 c#逐行读取txt文本,但是每一行里面数据之间空格数量不同
  • ¥50 如何openEuler 22.03上安装配置drbd
  • ¥20 ING91680C BLE5.3 芯片怎么实现串口收发数据
  • ¥15 无线连接树莓派,无法执行update,如何解决?(相关搜索:软件下载)
  • ¥15 Windows11, backspace, enter, space键失灵