2017-08-11 40 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網址