UUID值作为函数参数-Uncaught SyntaxError

huangapple go评论57阅读模式
英文:

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:

&lt;script&gt;
    function deleteBl(id){
        fetch(&#39;&#39;, {
            method: &#39;DELETE&#39;,
            headers: {
                &#39;X-CSRFToken&#39;: &#39;{{ csrf_token }}&#39;
            },
            body: JSON.stringify({
                &#39;id&#39;: id
            }),
            credentials: &#39;same-origin&#39;,
        })
    }

&lt;/script&gt;

and below Django template:

    {% for item in bl_query_set %}
    &lt;tr&gt;
        &lt;th scope=&quot;row&quot;&gt;{{ forloop.counter }}&lt;/th&gt;
        &lt;td&gt;{{ item.bl_number }}&lt;/td&gt;
        &lt;td class=&quot;text-center&quot;&gt;{{ item.cargo_name }}&lt;/td&gt;
        &lt;td&gt;{{ item.cargo_quantity }} {{ item.cargo_measurement }}&lt;/td&gt;
        &lt;td&gt;
            &lt;a onclick=&quot;deleteBl({{ item.id }})&quot;&gt;
                &lt;i class=&quot;fas fa-trash-alt&quot;&gt;&lt;/i&gt;
            &lt;/a&gt;
        &lt;/td&gt;
    &lt;/tr&gt;
    {% 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":

UUID值作为函数参数-Uncaught SyntaxError

How I can resolve mentioned issue?

答案1

得分: 2

你需要将其作为字符串传递:

<pre><code>&lt;a onclick=&quot;deleteBl(<b>'{{ item.id }}'</b>)&quot;&gt;</code></pre>

英文:

You need to pass it as a string:

<pre><code>&lt;a onclick=&quot;deleteBl(<b>'{{ item.id }}'</b>)&quot;&gt;</code></pre>

huangapple
  • 本文由 发表于 2023年8月8日 20:14:45
  • 转载请务必保留本文链接:https://go.coder-hub.com/76859463.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定