weixin_33705053 2017-01-07 07:32 采纳率: 0%
浏览 36

$ .ajax req.data在哪里? [重复]

This question already has answers here:
                </div>
            </div>
                    <div class="grid--cell mb0 mt4">
                        <a href="/questions/4295782/how-to-process-post-data-in-node-js" dir="ltr">How to process POST data in Node.js?</a>
                            <span class="question-originals-answer-count">
                                (28 answers)
                            </span>
                    </div>
            <div class="grid--cell mb0 mt8">Closed <span title="2017-01-07 11:54:00Z" class="relativetime">3 years ago</span>.</div>
        </div>
    </aside>

I am using node.js. It's a fairly simple file arrangement on a local server.

<h3>To Do!</h3>
<ol class="pending>
<li><a class="pending_task" href="localhost://nextstep.html">Task Three</a></li>
<li><a class="pending_task" href="localhost://nextstep.html">Task Five</a></li>
<li><a class="pending_task" href="localhost://nextstep.html">Task Six</a></li>
</ol>

and

<h3>Completed!</h3>
<ol class="completed">
<li><a class="completed_task" href="localhost://nextstep.html">Task One</a></li>
<li><a class="completed_task" href="localhost://nextstep.html">Task Two</a></li>
<li><a class="completed_task" href="localhost://nextstep.html">Task Four</a></li>
</ol>

Are lists on the same page. I finally have the ajax call working as desired, and now I am in the newest glitch.

The $.ajax redirects to the proper url and the connect is made, but there is no data when I do a console.log(req.data)

The AJAX call:

var newData = "<ol class=\"pending\">" + $('ol.pending').html() + 
            "</ol>" + "<ol class=\"completed\">" + $('ol.completed').html() + "<\ol>";

        $.ajax({
            method:'POST',
            data: newData,
            url: ajax_url,
            success: function(result, status, jqxhr){
                console.log("Response: " + jqxhr.responseText);
                console.log("POST status: " + status);
            },
            dataType: 'html',
        });

The app.js

http.createServer(function(req, res){
  if ((req.method === 'POST') && (req.url === '/result')){
        console.log("Request Data: " + req.data);
        res.statusCode = 200;
        res.end("Message Received!");
  }
 }).listen(port, serverURL);

My intended result is to rewrite the originating file with the new arrangement of completed/pending, after writing the old arrangement to a new file name (TODO_date_time.html), which I can't do yet because the generated data, passed to the AJAX call is not being transmitted. Or I am not accessing it preoperly.

What I get though is:

[nodemon] starting `node app.js`
Starting Web Server at local:4000
Request Data: undefined

The console.log in the browser is all good:

Browser console.log

Not sure what's wrong with my work at this point.

</div>
  • 写回答

2条回答 默认 最新

  • weixin_33701251 2017-01-07 10:50
    关注

    If you only need functionality, the suggested duplicate question above has a detailed explanation of simplifying a http server with Express.

    Otherwise, the problem is that the data is not available yet because it comes in as chunks. To remedy this, event listeners are necessary inside where you detect that it's a POST request to the /result endpoint.

    http.createServer(function(req, res) {
        if ((req.method === 'POST') && (req.url === '/result')) {
            console.log("POST request to /result received");
    
            req.on('data', function(chunk) {
                console.log('Received data: ', chunk.toString());
            });
    
            req.on('end', function() {
                res.statusCode = 200;
                res.end("Message Received!");
            });
    
        }
    }).listen(port, serverURL);
    

    Also, here's a link to a JSFiddle

    评论

报告相同问题?

悬赏问题

  • ¥15 如何在scanpy上做差异基因和通路富集?
  • ¥20 关于#硬件工程#的问题,请各位专家解答!
  • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
  • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
  • ¥30 截图中的mathematics程序转换成matlab
  • ¥15 动力学代码报错,维度不匹配
  • ¥15 Power query添加列问题
  • ¥50 Kubernetes&Fission&Eleasticsearch
  • ¥15 報錯:Person is not mapped,如何解決?
  • ¥15 c++头文件不能识别CDialog