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

报告相同问题?

悬赏问题

  • ¥90 请问,这个视频播放软件的名称,用过的朋友请给答案
  • ¥15 运筹优化,gurobi,python
  • ¥15 基于python的电影系统推荐
  • ¥20 springmvc重定向和返回json
  • ¥15 数学建模——参会安排怎么做
  • ¥15 电脑键盘实现触摸功能
  • ¥25 matlab无法将表达式转换为双数组怎么解决?
  • ¥15 单片机汇编语言相关程序
  • ¥20 家用射频美容仪技术规格
  • ¥15 大家帮我看看为什么错了