【问题标题】:Django Ajax not entrering in the functionDjango Ajax 没有进入函数
【发布时间】:2015-05-03 20:29:43
【问题描述】:

当我按下链接时,程序应该转到 ajax 函数来更新数据库而无需重新加载页面。 问题是当链接被按下时没有进入 JS 函数(不显示警报)

 <a href="#" onclick="xpto2()" >Remind later</a>
<script>    
function xpto2(){
     alert("hello");
     $.ajax({
        url: 'update-notify-status-noshow',
        data: { postdata1: {{ n.id }} }, 
        dataType: 'html', 
        type: 'get', )
        success: function(output) {
        alert(output); 
        }
    });
   </script>

【问题讨论】:

  • 不显示任何警报?控制台中抛出了哪些错误?函数是否在全局范围内?一些基本的故障排除信息会有所帮助。如果您不知道如何...对使用浏览器开发工具和控制台进行一些研究
  • 不知道警告和控制台中显示的错误是:SyntaxError: expected expression, got '}' ReferenceError: xpto is not defined
  • 使用代码 linter(在 IDE 中或在线)检查您的语法错误。当有许多工具可以找到它们时,它们永远不应该被带到这里作为问题。如果语法错误出现在 ajax 响应中,那就完全是另一个问题了

标签: javascript jquery html ajax django


【解决方案1】:

修复您的 xpto2 功能。存在语法错误。应该是这样的。

<script>    
        function xpto2() {
           alert("hello");
           $.ajax({
            url: 'update-notify-status-noshow',
            data: { postdata1: {{ n.id }} },
            dataType: 'html', 
            type: 'get',

            success: function(output) {
                alert(output); 
            }
        });
       }
   </script>

确保 jQuery 存在。

保持浏览器控制台打开以获得更好的洞察力。

这是一个完整的模板供您尝试。

更新

<!DOCTYPE html>
<html>
<head>
    <title></title>
    <script src="//ajax.googleapis.com/ajax/libs/jquery/1.11.0/jquery.min.js"></script>
    <script>    
        function xpto2() {
           alert("hello");
           $.ajax({
            url: 'update-notify-status-noshow',
            data: { postdata1: {{ n.id }} },
            dataType: 'html', 
            type: 'get',

            success: function(output) {
                alert(output); 
            }
        });
           }
   </script>
</head>
<body>
    <a href="#" onclick="xpto2()" >Remind later</a>
</body>
</html>

【讨论】:

  • 错误是:SyntaxError: expected expression, got '}' ReferenceError: xpto2() is not defined
  • 同样的错误...我导入了那个库。我注意到的一件事是,当我没有 ajax,只有警报时,它可以工作。使用 ajax 函数说 ReferenceError: xpto2() is not defined
  • 你是否包含两个版本的jQuery或xpto2函数?
  • 一、点击链接下载jquery库
猜你喜欢
  • 1970-01-01
  • 2017-12-26
  • 1970-01-01
  • 1970-01-01
  • 2014-06-28
  • 1970-01-01
  • 2021-10-27
  • 2014-08-14
  • 1970-01-01
相关资源
最近更新 更多