UUID值作为函数参数-Uncaught SyntaxError

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

UUID value as function argument - Uncaught SyntaxError

问题

我有一个函数,它以"id"作为参数:

  1. <script>
  2. function deleteBl(id){
  3. fetch('', {
  4. method: 'DELETE',
  5. headers: {
  6. 'X-CSRFToken': '{{ csrf_token }}'
  7. },
  8. body: JSON.stringify({
  9. 'id': id
  10. }),
  11. credentials: 'same-origin',
  12. })
  13. }
  14. </script>

和下面的Django模板:

  1. {% for item in bl_query_set %}
  2. <tr>
  3. <th scope="row">{{ forloop.counter }}</th>
  4. <td>{{ item.bl_number }}</td>
  5. <td class="text-center">{{ item.cargo_name }}</td>
  6. <td>{{ item.cargo_quantity }} {{ item.cargo_measurement }}</td>
  7. <td>
  8. <a onclick="deleteBl({{ item.id }})">
  9. <i class="fas fa-trash-alt"></i>
  10. </a>
  11. </td>
  12. </tr>
  13. {% endfor %}

如果我使用默认的Id作为主键,就不会出现任何控制台错误。但是我需要使用UUID作为主键,在这种情况下,我会收到一个错误:"Uncaught SyntaxError: Invalid or unexpected token"。如何解决这个问题?

英文:

I have a function which takes an "id" as argument:

  1. &lt;script&gt;
  2. function deleteBl(id){
  3. fetch(&#39;&#39;, {
  4. method: &#39;DELETE&#39;,
  5. headers: {
  6. &#39;X-CSRFToken&#39;: &#39;{{ csrf_token }}&#39;
  7. },
  8. body: JSON.stringify({
  9. &#39;id&#39;: id
  10. }),
  11. credentials: &#39;same-origin&#39;,
  12. })
  13. }
  14. &lt;/script&gt;

and below Django template:

  1. {% for item in bl_query_set %}
  2. &lt;tr&gt;
  3. &lt;th scope=&quot;row&quot;&gt;{{ forloop.counter }}&lt;/th&gt;
  4. &lt;td&gt;{{ item.bl_number }}&lt;/td&gt;
  5. &lt;td class=&quot;text-center&quot;&gt;{{ item.cargo_name }}&lt;/td&gt;
  6. &lt;td&gt;{{ item.cargo_quantity }} {{ item.cargo_measurement }}&lt;/td&gt;
  7. &lt;td&gt;
  8. &lt;a onclick=&quot;deleteBl({{ item.id }})&quot;&gt;
  9. &lt;i class=&quot;fas fa-trash-alt&quot;&gt;&lt;/i&gt;
  10. &lt;/a&gt;
  11. &lt;/td&gt;
  12. &lt;/tr&gt;
  13. {% 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:

确定