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 如何让企业微信机器人实现消息汇总整合
  • ¥50 关于#ui#的问题:做yolov8的ui界面出现的问题
  • ¥15 如何用Python爬取各高校教师公开的教育和工作经历
  • ¥15 TLE9879QXA40 电机驱动
  • ¥20 对于工程问题的非线性数学模型进行线性化
  • ¥15 Mirare PLUS 进行密钥认证?(详解)
  • ¥15 物体双站RCS和其组成阵列后的双站RCS关系验证
  • ¥20 想用ollama做一个自己的AI数据库
  • ¥15 关于qualoth编辑及缝合服装领子的问题解决方案探寻
  • ¥15 请问怎么才能复现这样的图呀