2017-05-06 274 views
0

尝试了很多示例,花费了大量时间,但它只是没有解决。如果有人能够在这种情况下帮助我,那将是非常棒的。所以,我想要做的是从p#lessonText.lesson(style='margin-left:1%;' name='desc') #{item.description} 获得价值我需要#{item.description}传递给我的服务器端。从客户端到服务器端节点的传输值js

这是在服务器端的我的客户端代码

form#myForm() 
    .container 
    ul 
     each item, index in items 
     #allLessons.container 
      .lessonsWrap 
      h3.lesson #{index+1} . Lesson | 
      p#lessonText.lesson(style='margin-left:1%;' name='desc') #{item.description} 
      button#butPam.btn.btn-primary(type='submit') Begin 
      .lesDescription 
       p(style='margin-left:1%;') Lecturer: #{item.author} 
       p(style='margin-left:1%;') Level: #{item.level} 

       script. 
       $('#myForm').on('submit', function(event){ 

        var createVar = $('#lessonText').text(); 
        alert(createVar); 
        $.ajax({ 
        url: '/qVal', 
        method: 'post', 
        contentType: 'application/json', 
        data: JSON.stringify({desc: createVar.val()}), 
        async: true, 
        success: function(response){ 
         console.log(response); 

        } 
        }) 

       }); 

这里现在我只是想看看,如果我可以创建一个变量,它是从客户端通过。

router.post('/qVal', function(req,res){ 
    console.log("server"); 
    var description = req.body.desc; 
    console.log(typeof(desc)); 
    console.log(desc); 
}); 

也许我错了使用方法,也许我应该使用GET方法呢? 重要的是说,它看起来我的router.post不起作用。当我写console.log(“服务器”),在控制台单词“服务器”没有出现。

+0

所以你的'console.log(“服务器”);'被解雇? – gaganshera

+0

不是,它不 –

+0

检查您的浏览器的ajax调用的网络选项卡是否触发,并检查请求中的URL。我猜可能是/ qVal路径 – gaganshera

回答

0
var querystring = require('querystring'); 

router.post('/qVal', function(req,res){ 
    req.on('data',function(data){ 
      data = querystring.parse(data.toString('utf-8')); 
      var description = data.desc; 
      console.log(typeof(desc)); 
      console.log(desc); 
    }); 
} 
+0

看起来像router.post没有开火 –

+0

需要更多的代码才能理解否则访问我的github回购https://github.com/SaifAnsari96/Rest-Server-with-pure-Node.js/blob/master/server.js – Saif

0

您将错误的方法发送到服务器的数据部分您将JSON转换为字符串,但在服务器上,您将得到JSON值,这就是我所看到的原因。

前端Ajax请求

$.ajax({ 
        url: '/qVal', 
        method: 'post', 
        contentType: 'application/json', 
        data: {desc: createVar.val()}, 
        async: true, 
        success: function(response){ 
         console.log(response); 
        } 
        }) 

节点的js

router.post('/qVal', function(req,res){ 
    console.log("server"); 
    console.log(JSON.stringify(req.body)); 
    console.log(req.body.desc) 
} 
+0

它看起来像router.post没有启动。控制台不显示“服务器” –

0

当你使用JSON.stringify它分析你JSON为字符串,如果它有" "


如果您将添加"desc"这样的:

客户

$.ajax({ 
     url: '/qVal', 
     method: 'post', 
     data: { 
        "desc": createVar.val() 
       }, 
     async: true, 
     success: function(response){ 
     console.log(response); 
     } 
     }) 



服务器

router.post('/qVal', function(req, res){ 
     console.log(JSON.stringify(req.body)); 
     console.log(req.body.desc) 
} 

它会以正确的方式发送到服务器。

+0

尽你所能,但这是我得到的。 http://i.imgur.com/KQJhiwa.png它看起来像我从客户端 –

+0

6空值,也许你传递空值,尝试发送“desc”:“示例”,如果它的工作,那么你发送不正确的错误 –

+0

嗯,如果我随机写了一些东西,例如'data:{“desc”:“test”},'它起作用,并且我的服务器端的名字为“test”。那么,也许你知道我怎么能通过像h2,p或其他任何标签来传递价值? –

相关问题