weixin_33714884 2016-02-08 16:24 采纳率: 0%
浏览 26

Ajax请求错误404

Hello again and thanks in advance. Let me first show you the code and the problem that I am trying to resolve:

<span class='dropdown' id='status'>
              <?php
                switch ($status) {
                  case "unassigned":
                  echo "<button type='button'  data-toggle='dropdown' id='edit' aria-haspopup='true' aria-expanded='true' class='btn btn-danger dropdown-toggle'> Status: " . $status . "<span class='caret'></span></button>";
                  echo "<ul class='dropdown-menu'   aria-labelledby='edit'>";
                  echo "<li><a class='editStatus' href='pending'>Pending</a></li>";
                  echo "</ul>";
                  break;

                  case "pending":
                  echo "<button type='button'  data-toggle='dropdown' id='edit' aria-haspopup='true' aria-expanded='true' class='btn btn-danger dropdown-toggle'> Status: " . $status . "<span class='caret'></span></button>";
                  echo "<ul class='dropdown-menu'   aria-labelledby='edit'>";
                  echo "<li><a class='editStatus' href='attending'>Attending</a></li>";
                  echo "<li><a class='editStatus' href='followup'>Follow Up</a></li>";
                  echo "<li><a class='editStatus' href='closed'>Closed</a></li>";
                  echo "</ul>";
                  break;

                  case "attending":
                  echo "<button type='button'  data-toggle='dropdown' id='edit' aria-haspopup='true' aria-expanded='true' class='btn btn-danger dropdown-toggle'> Status: " . $status . "<span class='caret'></span></button>";
                  echo "<ul class='dropdown-menu'   aria-labelledby='edit'>";
                  echo "<li><a class='editStatus' href='pending'>Pending</a></li>";
                  echo "<li><a class='editStatus' href='followup'>Follow Up</a></li>";
                  echo "<li><a class='editStatus' href='closed'>Closed</a></li>";
                  echo "</ul>";
                  break;

                  case "followup":
                  echo "<button type='button'  data-toggle='dropdown' id='edit' aria-haspopup='true' aria-expanded='true' class='btn btn-danger dropdown-toggle'> Status: " . $status . "<span class='caret'></span></button>";
                  echo "<ul class='dropdown-menu'   aria-labelledby='edit'>";
                  echo "<li><a class='editStatus' href='closed'>Closed</a></li>";
                  echo "</ul>";
                  break;

                  case "closed":
                  echo "<button type='button'  data-toggle='dropdown' id='edit' aria-haspopup='true' aria-expanded='true' class='btn btn-danger dropdown-toggle'> Status: " . $status . "<span class='caret'></span></button>";
                  echo "<ul class='dropdown-menu'   aria-labelledby='edit'>";
                  echo "<li><a class='editStatus' href='pending'>Pending</a></li>";
                  echo "<li><a class='editStatus' href='attending'>Attending</a></li>";
                  echo "<li><a class='editStatus' href='followup'>Follow Up</a></li>";
                  echo "</ul>";
                  break;
                }
              ?>        
                  </span> 

I have this span in my php script that includes a dropdown button with options which represent the different status of the message being displayed in this page, and through ajax I send this info to another php script

<script>
var phpvar1 = "<?php echo $frompost_id_sanitized; ?>";
var phpvar = "<?php echo $status; ?>";

$('a.editStatus').click(function(event) {
event.preventDefault();
    var statusJs = $(this).attr("href");

    alert('Change Status of ticket to: ' + statusJs)
    $.post('ajaxChangeStatus.php', {status: statusJs, id: phpvar1, initialStatus: phpvar}, function(data) {
        $('#status').html(data)
    });
});

Finally the 2nd php script adds data to db and sends back some html to the 1st php script like this (some code is omitted)

switch ($frompost_status_sanitized) {
    case "pending":
    echo "<button type='button'  data-toggle='dropdown' id='edit' aria-haspopup='true' aria-expanded='true' class='btn btn-danger dropdown-toggle'> Status: " . $frompost_status_sanitized . "&nbsp;<span class='caret'></span></button>";
    echo "<ul class='dropdown-menu'   aria-labelledby='edit'>";
    echo "<li><a class='editStatus' href='attending'>Attending</a></li>";
    echo "<li><a class='editStatus' href='followup'>Follow Up</a></li>";
    echo "<li><a class='editStatus' href='closed'>Closed</a></li>";
    echo "</ul>";
    break;

    case "attending":
    echo "<button type='button'  data-toggle='dropdown' id='edit' aria-haspopup='true' aria-expanded='true' class='btn btn-danger dropdown-toggle'> Status: " . $frompost_status_sanitized . "&nbsp;<span class='caret'></span></button>";
    echo "<ul class='dropdown-menu'   aria-labelledby='edit'>";
    echo "<li><a class='editStatus' href='pending'>Pending</a></li>";
    echo "<li><a class='editStatus' href='followup'>Follow Up</a></li>";
    echo "<li><a class='editStatus' href='closed'>Closed</a></li>";
    echo "</ul>";
    break;

    case "followup":
    echo "<button type='button'  data-toggle='dropdown' id='edit' aria-haspopup='true' aria-expanded='true' class='btn btn-danger dropdown-toggle'> Status: " . $frompost_status_sanitized . "&nbsp;<span class='caret'></span></button>";
    echo "<ul class='dropdown-menu'   aria-labelledby='edit'>";
    echo "<li><a class='editStatus' href='closed'>Closed</a></li>";
    echo "</ul>";
    break;

    case "closed":
    echo "<button type='button'  data-toggle='dropdown' id='edit' aria-haspopup='true' aria-expanded='true' class='btn btn-danger dropdown-toggle'> Status: " . $frompost_status_sanitized . "&nbsp;<span class='caret'></span></button>";
    echo "<ul class='dropdown-menu'   aria-labelledby='edit'>";
    echo "<li><a class='editStatus' href='pending'>Pending</a></li>";
    echo "<li><a class='editStatus' href='attending'>Attending</a></li>";
    echo "<li><a class='editStatus' href='followup'>Follow Up</a></li>";
    echo "</ul>";
    break;
}

Everything seems to works ok:

The idea is that I have (in the 1st php script) a dropdown button with options(representing the status of the message displayed). When an option(eg the status is changed) is selected(click event) I send through ajax to the 2nd php script the option selected(the new status), then perform some actions and return the appropriate html (dropdown button with different oprions) to the 1st php script.

The Problem If then I select another option (eg if I try to change the status again without reloading page) then I get a 404 error, because when click in the option instead of having the click event triggered and then event.preventdefault ......(as it should happen), the app reads the href attribute[which is used for sending data to the 2nd php script like this: var statusJs = $(this).attr("href");] and tries to load a page which causes the 404 error http://localhost:8888/ticketing/pending

Normally the js script should read the value from the href attribute, prevent the default action(eg load page that causes 404), send data to the 2nd php script and replace the span contants with the html returned

What is wrong?

  • 写回答

1条回答 默认 最新

  • 普通网友 2016-02-09 08:20
    关注

    Delegation is the solution

    https://learn.jquery.com/events/event-delegation/

    Thanks a lot

    评论

报告相同问题?

悬赏问题

  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度
  • ¥30 关于#r语言#的问题:如何对R语言中mfgarch包中构建的garch-midas模型进行样本内长期波动率预测和样本外长期波动率预测
  • ¥15 ETLCloud 处理json多层级问题
  • ¥15 matlab中使用gurobi时报错
  • ¥15 这个主板怎么能扩出一两个sata口
  • ¥15 不是,这到底错哪儿了😭
  • ¥15 2020长安杯与连接网探
  • ¥15 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么