英文:
UUID value as function argument - Uncaught SyntaxError
问题
我有一个函数,它以"id"作为参数:
<script>
    function deleteBl(id){
        fetch('', {
            method: 'DELETE',
            headers: {
                'X-CSRFToken': '{{ csrf_token }}'
            },
            body: JSON.stringify({
                'id': id
            }),
            credentials: 'same-origin',
        })
    }
</script>
和下面的Django模板:
{% for item in bl_query_set %}
<tr>
    <th scope="row">{{ forloop.counter }}</th>
    <td>{{ item.bl_number }}</td>
    <td class="text-center">{{ item.cargo_name }}</td>
    <td>{{ item.cargo_quantity }} {{ item.cargo_measurement }}</td>
    <td>
        <a onclick="deleteBl({{ item.id }})">
            <i class="fas fa-trash-alt"></i>
        </a>
    </td>
</tr>
{% endfor %}
如果我使用默认的Id作为主键,就不会出现任何控制台错误。但是我需要使用UUID作为主键,在这种情况下,我会收到一个错误:"Uncaught SyntaxError: Invalid or unexpected token"。如何解决这个问题?
英文:
I have a function which takes an "id" as argument:
<script>
    function deleteBl(id){
        fetch('', {
            method: 'DELETE',
            headers: {
                'X-CSRFToken': '{{ csrf_token }}'
            },
            body: JSON.stringify({
                'id': id
            }),
            credentials: 'same-origin',
        })
    }
</script>
and below Django template:
    {% for item in bl_query_set %}
    <tr>
        <th scope="row">{{ forloop.counter }}</th>
        <td>{{ item.bl_number }}</td>
        <td class="text-center">{{ item.cargo_name }}</td>
        <td>{{ item.cargo_quantity }} {{ item.cargo_measurement }}</td>
        <td>
            <a onclick="deleteBl({{ item.id }})">
                <i class="fas fa-trash-alt"></i>
            </a>
        </td>
    </tr>
    {% endfor %}
There are no any console errors if I use default Id as primary key. But I need to use UUID as a primary key and in that case I get an error: "Uncaught SyntaxError: Invalid or unexpected token":
How I can resolve mentioned issue?
答案1
得分: 2
你需要将其作为字符串传递:
<pre><code><a onclick="deleteBl(<b>'{{ item.id }}'</b>)"></code></pre>
英文:
You need to pass it as a string:
<pre><code><a onclick="deleteBl(<b>'{{ item.id }}'</b>)"></code></pre>
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。



评论