我试图让某个页面上的某些文字根据从下拉栏中选择的内容进行更改,但是无法让我的javascript调用我的Django函数来确定文本以便占位符文字永远不会被取代。
JS:
function updateDateRange(){
text = "";
$.ajax({
url:'/report/api_report_dates/',
data: {'option':selection_criteria_date[0]},
type: 'POST',
error: function(){alert('Error!');},
success: function(){
text = "Date Range: " + infoDict['start_date'] + " to " + infoDict['end_date'];
},
});
urls.py:
(r'^report/$', 'report_view.view_report'),
(r'^report/api_report', 'report_view.api_report'),
(r'^report/api_report_dates', 'report_view.api_report_dates'),
report_view.api_report_dates:
def api_report_dates(request):
infoDict = {}
infoDict['start_date'] = (gets start date from request.option, else is "")
infoDict['end_date'] = (gets end date from request.option, else is "")
return HttpResponse(json.dumps(infoDict), content_type="application/json")
占位符文字会显示在选择某个选项之前应该显示的位置,并且警告“完成!”#39;出现在我希望的时候。但是它永远不会改变文本,我在report_view.api_report_dates的开头设置了一个断点,但是除了页面加载之外,调试器永远不会命中它。
非常感谢任何帮助!
编辑: dhana的回复帮助了我,更新了代码以反映当前状态。我现在的问题是,ajax既不会出现“错误”错误。或者'成功'条款。
如果版本很重要,这个站点使用Django 1.2和JQuery 1.4.2
答案 0 :(得分:0)
你应该这样写,
function updateDateRange(){
$.ajax({
url:'/report/api_report_dates/',
data: {'option':selection_criteria_date[0]},
type: 'POST',
error: function(){alert('Error!');},
success: function(){
alert('Done!');
text = "Hello!"
if (infoDict['start_date'] != '') {
text = "Date Range: " + infoDict['start_date'] + " to " + infoDict['end_date'];
}
},
}); //returns infoDict with 'start_date' and 'end_date'
在urls.py文件中添加此
(r'^report/api_report_dates/$', 'report_view.api_report_dates'),
在report_view.api_report_dates中:
import json
def api_report_dates(request):
infoDict = {}
infoDict['start_date'] = "some date"
infoDict['end_date'] = "some date"
return HttpResponse(json.dumps(infoDict))