0

/ node_api(python + djnago)でデータを送信する必要があります。socket.ioでnode.jsを使用します。

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...そしてGETnullも

私を助けてください!!!

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 に答える