S
S
Socrates2018-03-19 11:40:08
Django
Socrates, 2018-03-19 11:40:08

Get responseText from json file?

Bottom line: I get the wrong responseText when trying to parse a json file.
What is:
1. Application in the project: "printing"
5aaf750b8cd70248456888.jpeg
2. Templates: printings.html
3. In the template printings.html I include main.js

{% extends 'base.html' %}
{% load static %}
{% block content %}
    <h1>Типографии</h1>

    {% if print_shops %}
        {% if cityes %}
            <ul class="print-shops-cityes">
                {% for city in cityes %}
                    <li><a href="{{ city.name }}">{{ city.name }}{{city.count}}</a></li>
                {% endfor %}
            </ul>
        {% endif %}
        {% include '../includes/print_shop_list.html' %}
    {% else %}
        <p>Нет типографий</p>
    {% endif %}
    <script src="{% static 'printing/main.js' %}"></script>
{% endblock %}

4. main.js (script for this application, in which I try to pull data from data.json and pass it to the template)
// Подключаем json файл с классами бетона для калькулятора
var xhr = new XMLHttpRequest();
// 2. Конфигурируем его: GET-запрос на URL 'calc_data.json'
xhr.open('GET', 'cityes.json', false);
// xhr.responseType = "JSON";
console.log('xhr.responseType',xhr.responseType);
// 3. Отсылаем запрос
xhr.send();
// 4. Если код ответа сервера не 200, то это ошибка
if (xhr.status != 200) {
// обработать ошибку
console.log( xhr.status + ': ' + xhr.statusText ); // пример вывода: 404: Not Found
} else {
    // вывести результат
    //var items = JSON.parse(xhr.responseText); // responseText -- текст ответа.
    console.log(xhr.responseText);
}

5. And the actual file cities.json
{
"data":[
{
'key':'value',
'key':'value'
},
{
'key':'value',
'key':'value'
}
]
}
But, with a standard attempt to get a json file and parse it
in my console.log(xhr.responseText); not json data is displayed, but the entire page, and not the page where I connect the script, but the detailed page.
What could be the problem?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
S
Sergey Gornostaev, 2018-03-19
@Karmov69

xhr.open('GET', '/static/printing/cityes.json', false);

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question