【问题标题】:Do not send GET request on clicking a link不要在单击链接时发送 GET 请求
【发布时间】:2014-04-27 00:46:45
【问题描述】:

[编辑] 我有以下链接:

<a href="" class="undo_feedback">Undo</a>

我在单击链接时执行 ajax 请求:

$('table').on('click', '.undo_feedback', function(e){
    // some code here
    $.ajax({
      type: "POST",
      data: {'_method': 'delete'},
      url: //some url
    });
    e.preventDefault();
  });

它给出了错误cannot GET '//some url'。 POST 请求执行成功。我怎样才能摆脱这个错误? 我尝试过使用return falsee.stopPropagation()e.stopImmediatePropagation(),但它们似乎都不起作用。任何帮助将非常感激。在此先感谢:)

【问题讨论】:

  • // some code here 上方放置一个alert("test"); 以检查函数是否被调用。是吗?

标签: javascript jquery ajax html node.js


【解决方案1】:
$('table').on('click', '.undo_feedback', function(e){
   // some code here
   e.preventDefault();    
   $.ajax({
      type: "POST",
      method : "delete",
      url: //some url
   });
});

e.preventDefault();放在上面

希望对你有帮助

【讨论】:

  • 我也试过了,可惜没用。
【解决方案2】:

你需要定义变量delete_url

$('table').on('click', '.undo_feedback', function(e){
    // some code here
--> delete_url = '/mydir/mydelete.php'; <--- LOOK HERE
    $.ajax({
      type: "POST",
      data: {'_method': 'delete'},
      url: delete_url,
    });
    e.preventDefault();
  });

我个人更喜欢使用 jQuery.post 方式而不是通过 ajax...

jQuery.post(delete_url,
                {
                _method : 'delete'
                }
            ,
            function(data)
                {
                window.alert(data);
                }
            );

【讨论】:

    【解决方案3】:

    您是否将 url 分配给 delete_url

    试试这个

    //assign url to the variable
    var delete_url="google.com/post";
    $('table').on('click', '.undo_feedback', function(e){
    // some code here
    $.ajax({
      type: "POST",
      data: {'_method': 'delete'},
      url: delete_url,
    });
    e.preventDefault();
    });
    

    【讨论】:

    • 为什么要添加一个已经存在的答案?我有什么好处吗?还有,为什么要在函数作用域之外声明变量呢?
    • lolz 抱歉,我在发帖后看到了。如果你想让我删除它,我可以。 :)
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-08-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-03-26
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多