0

我应该在 /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 太

请帮我!!!

4

1 回答 1

0

你检查错误代码了吗?可能是 CSRF 检查问题。

from django.views.decorators.csrf import csrf_exempt

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

你能试试这个吗?

于 2013-02-04T20:44:01.713 回答