我想發送一個ajax請求到我的views.py,但我不知道如何使用路徑。 我的意見位於我的服務器上/home/pycode/main/main/apps/builder/views.py.
我發送請求的頁面位於/home/dbs/www/python.html
我需要向我的urls.py添加內容嗎?django簡單的ajax請求
views.py
#!/usr/bin/env python26
from django.http import HttpResponse
def main(request):
return HttpResponse("from python with love")
python.html jQuery的AJAX
<script language="JavaScript">
$(document).ready(function() {
$("#myform").submit(function() {
var myCheckboxes = new Array();
$("input:checked").each(function() {
myCheckboxes.push($(this).val());
});
$.ajax({
type: "POST",
url: '/main',
data: { myCheckboxes:myCheckboxes },
success: function(response){
alert(response);
}
});
return false;
});
});
</script>
很好的回答。到OP:如果你在'/ main /'中包含一個斜線,確保將它添加到javascript中的url中,否則你可能會遇到問題。 – Alasdair
但我的應用程序需要位於'/ home/dbs/www /'與我的網站,還是它只需要在服務器的某個地方? – user1442957
@ user1442957你的應用程序可以位於任何地方。如果您使用的是開發服務器,當您向其發送請求時,您的應用程序將被初始化爲默認端口:8000,因此任何請求'localhost:8000'都將進入您的應用程序 – dm03514