2017-08-11 41 views
0

我是新的AJAX和jQuery,我试图发布一条新的评论到'campground/campground_.id'与我的ajax.js文件,但我无法找到如何编写与URL的URL。 .. 我tryed:Ajax + jQuery发布网址与Id

$('#new-comment-form').submit(function(e){ 
    e.preventDefault(); 
    var comment = $(this).serialize(); 
    $.post('/campgrounds/' + campground._id, comment, function(data){ 
     debugger; 
    }); 
}) 

错误营地没有定义

$('#new-comment-form').submit(function(e){ 

    e.preventDefault(); 
    var comment = $(this).serialize(); 
    $.post('/campgrounds/campground._id', comment, function(data){ 
     debugger; 
    }); 
}) 

错误POST未发现

$('#new-comment-form').submit(function(e){ 
    e.preventDefault(); 
    var comment = $(this).serialize(); 

    $.post('/campgrounds/<%= campground._id %>', comment, function(data){ 
     debugger; 
    }); 
}) 

错误错误请求

有人可以帮助我如何把它写正确?

+0

你在哪里得到来自ID? – Dekel

+0

''/ campgrounds /'+ campground._id'应该可以工作,假设'campground'对象被正确定义并且具有'_id'属性。在'$ .post'之前插入'console.log(campground)并告诉我们它在控制台中的含义。 – nurdyguy

+0

这取决于'campground._id'究竟是什么?它是一个变量,是URL的一部分,通过模板语言从服务器端来的东西? – adeneo

回答

0

你必须设置处理请求的服务器页面,如果你使用RRE本地服务器只是把nmae的文件,或者远程服务器把compltet网址