我正在努力尝试在我的模板中为Javascript分配动态变量。这是将数据传递回页面。目前这是硬编码的。我一直试图找出解决这个问题的最佳方法。这是我的代码:
JS
function MyModule.init() {
var myXHR = new XMLHttpRequest(),
seconds = (new Date()).getTime();
// Load my data
myXHR.addEventListener('readystatechange', function () {
var preXHR = new XMLHttpRequest(),
data;
if (myXHR.readyState === 4) {
data = JSON.parse(myXHR.responseText);
timestamp = data['timestamp'];
myData = data['my'];
// Load rep data
preXHR.addEventListener('readystatechange', function () {
if (preXHR.readyState === 4) {
repData = JSON.parse(preXHR.responseText)['reps'];
organizeData();
showMYTable();
}
});
// This hardcoded path is what I am trying to change, as it is bad
preXHR.open('GET', '/catalog/1/my-reps?t=' + seconds);
preXHR.send();
}
});
// This hardcoded path is what I am trying to change, as it is bad
myXHR.open('GET', '/catalog/1/my-data?t=' + seconds);
myXHR.send();
TEMPLATE
<script>
$(function(){
MyModule.init('{% url catalog.data %}');
});
</script>
网址
url(r'^(?P<company>.+)/(?P<name>.+)', 'data', name='catalog.data')
答案 0 :(得分:0)
您似乎已将网址传递到MyModule.init()
功能,但您在那里没有做任何事情。只需将其作为变量引用:
function MyModule.init(url) {
...
myXHR.open('GET', url + '?t=' + seconds);