简体   繁体   English

好像我的Ajax调用url不起作用

[英]It seems like my ajax call to url is not working

I want to display a confirmation dialog to user before they delete a record in the database. 我想在用户删除数据库中的记录之前向用户显示确认对话框。 I am developing this on my local PC webserver. 我正在本地PC网络服务器上进行开发。 Sort of like this: user selects record for deletion(php form) user clicks delete button(php submit button) a dialog box pops up saying do you want to delete record(jquery) Yes - Ajax call to another php page in same directory 有点像这样:用户选择要删除的记录(php形式)用户单击删除按钮(php提交按钮)弹出对话框,说要删除记录(jquery)是的-Ajax调用同一目录中的另一个php页面

I've tried this with both chrome and FF with no errors that I can see other than the resulting page is never called. 我已经尝试过使用chrome和FF进行此操作,没有错误,除了从未调用过生成的页面外,我看不到其他错误。 FYI, The de.php page works fine if I call it directly through the browser like so: 仅供参考,如果我直接通过浏览器像这样调用de.php页面,则该页面可以正常工作:

http://localhost/de.php?eid=61

Here are some snippets: 以下是一些摘要:

<script>
$(function() {
 $( "#dialog-confirm" ).dialog({
  resizable: false,
  height:340,
  modal: true,
  buttons: {
    "Delete Event": function() {
     $.ajax({
        url: '/de.php?eid=<?php echo $theEventId ?>',
        success: function(data) {
           alert('success');
        }, error: function (data) {
           alert('failed');
        }

      });
      $( this ).dialog( "close" );
      $('#myDiv').append('Trying to Delete');
    },
    Cancel: function() {
      $( this ).dialog( "close" );
      $('#myDiv').append('Delete Cancelled');
    }
  }
 });
});
</script>

The form code 表单代码

<form method="post" action="<?php echo htmlspecialchars($_SERVER['REQUEST_URI']) ?>" id="ee_form">

  <div id="myDiv"></div>

  <input type="submit" name="Update" value="Update Event" />
  <a href="index.php"><input type="button" value="Cancel" /></a>      
  <input type="submit" name="Delete" value="Delete Event <?php echo $theEventId ?>" />
</form>

The dialogue box comes up fine, with the correct eid. 对话框显示良好,带有正确的eid。 If I select the "Delete Event" button I get a "success" alert, dialog closes, "Trying to Delete" is printed to the screen fine, but no indication that de.php was ever called. 如果我选择“删除事件”按钮,则会收到“成功”警报,对话框关闭,屏幕上会很好地打印“正在尝试删除”,但没有迹象表明曾经调用过de.php。

Am I doing this correctly, what am I missing? 我是否正确执行此操作,我缺少什么?

Change the ajax section to post data. 更改ajax部分以发布数据。

Have the data in a form rather than echo'ing the JS. 使数据具有某种形式,而不是回显JS。

<form id="myform">
 <input type="hidden" value="<?php echo $theEventId ?>">
 <button type="submit" value="Delete">
</form>

Then in the JS, serialize the form. 然后在JS中序列化表单。

var form_data = $("#myform").serialize();    

Then post the serialized data. 然后发布序列化的数据。

$.ajax({
        url: '/de.php',
        type: 'POST',
        data: form_data,
        success: function(data) {
           alert('success');
        }, error: function (data) {
           alert('failed');
        }

      });

Use developer tools in google chrome to see if anything was posted and what it is. 使用Google Chrome中的开发者工具查看是否发布了任何内容。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM