【问题标题】:socket.io, send data (node.js)socket.io,发送数据(node.js)
【发布时间】:2013-01-19 09:16:13
【问题描述】:

我应该在 /node_api (python+djnago) 上发送数据,我使用 node.js 和 socket.io:

socket.on('send_message', function (message) {
        values = querystring.stringify({
            comment: message, // not null
            sessionid: socket.handshake.cookie['sessionid'],
        });

        var options = {
            host: 'localhost',
            port: 8000,
            path: '/node_api',
            method: 'POST',
            headers: {
                'Content-Type': 'application/x-www-form-urlencoded',
                'Content-Length': values.length
            }
        };

        var req = http.get(options, function(res){
            res.setEncoding('utf8');

            res.on('data', function(message){
                if(message != 'work'){
                    console.log('Message: ' + message);
                }
            });
        });

        req.write(values);
        req.end();

但在我的views.py中没有发送数据:

request.POST.get('comment','null') # null

请求没问题,但是为什么POST是空的?

如果我在 console.log(req) 我看

 method: 'GET',
  path: '/node_api',
  _events: { response: [Function] },
  _headers: 
   { 'content-type': 'application/x-www-form-urlencoded',
     'content-length': 52,
     host: 'localhost:8000' },
  _headerNames: 
   { 'content-type': 'Content-Type',
     'content-length': 'Content-Length',
     host: 'Host' },
  _header: 'GET /node_api HTTP/1.1\r\nContent-Type: application/x-www-form-urlencoded\r\nContent-Length: 52\r\nHost: localhost:8000\r\nConnection: keep-alive\r\n\r\n',

我不知道,但是方法是 GET...并且 GET null 也是

请帮帮我!!!

【问题讨论】:

标签: python django node.js socket.io


【解决方案1】:

您检查过错误代码吗?可能是 CSRF 检查问题。

from django.views.decorators.csrf import csrf_exempt

@csrf_exempt
def my_api_view(request):
    return HttpResponse("text")

你可以试试这个吗?

【讨论】:

    猜你喜欢
    • 2018-08-10
    • 1970-01-01
    • 2013-01-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-12-31
    • 2021-03-20
    相关资源
    最近更新 更多