javascript - 我的 ajax 根本不起作用?

标签 javascript jquery html ajax

我有一个 AJAX 函数(如下)。我正在尝试将输入从一页传递到另一页。

它将我重定向到另一个页面,但没有传递所需的值。

我不明白为什么它没有像我预期的那样工作。

我正在寻找另一种方法来做到这一点。任何帮助将不胜感激。

  //second page
  
  <?php
     $cuit = $_POST['cuit'];
    ?>
<input id="act" type="text" class="form-control" value="<?php echo $cuit ?>">

function mod(id, origen) {
  swal({
    title: 'Are you sure?',
    text: "You won't be able to revert this!",
    type: 'warning',
    showCancelButton: true,
    confirmButtonColor: '#3085d6',
    cancelButtonColor: '#d33',
    confirmButtonText: 'Yes, delete it!',
    cancelButtonText: 'No, cancel!',
    confirmButtonClass: 'btn btn-success',
    cancelButtonClass: 'btn btn-danger',
    buttonsStyling: false
  }).then(function() {
    if (origen == "perfiles") {
      $.post("api/eliminar_perfil.php", {
        id: id
      }, function(mensaje) {
        $("#tr_" + id).remove();
      });
    } else if (origen == "index") {
      $.ajax({
        type: "post",
        url: "perfiles.php",
        data: {
          cuit: $("#cuit").val()
        },
        success: function(result) {
          location.href = 'http://localhost/miramonteapp/perfiles.php';
        }
      });
    }
    swal('Deleted!', 'Your file has been deleted.', 'success')
  }, function(dismiss) {
    // dismiss can be 'cancel', 'overlay',
    // 'close', and 'timer'
    if (dismiss === 'cancel') {
      swal('Cancelled', 'Your imaginary file is safe :)', 'error')
    }
  })
}
<input id="cuit" type="text" class="form-control" onkeyup="searchCliente();">

<button id="btnBuscar" onclick="mod($('#cuit'), 'index');" type="button" class="btn btn-default" name="button">Buscar</button>

最佳答案

男孩,我认为你的解决方案更简单,如果你想使用参数重定向,则不需要 ajax 调用。

所以替换这个

 $.ajax({
               type : "post",
               url : "perfiles.php",
               data :  {cuit: $("#cuit").val()},
               success : function(result) {
                location.href = 'http://localhost/miramonteapp/perfiles.php';
               }
             });

通过这个

 location.href = 'http://localhost/miramonteapp/perfiles.php?cuit=' + 
 $("#cuit").val();

并在您的目标页面 perfiles.php

只需读取参数并使用它,您可以使用此作为指南在 JS 中读取参数 How to get the value from the GET parameters?

关于javascript - 我的 ajax 根本不起作用?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46207635/

相关文章:

javascript - PhoneGap Android 查询字符串问题

javascript - 考虑 seo 在滚动时淡入淡出动画(但只有一次)

javascript - 为什么文本在 CSS 无序列表菜单中的图像下方

javascript - 缩放 HTML Canvas 元素

html - 使用 ruby​​ link_to 方法无法正确显示 Bootstrap 导航栏下拉列表

javascript - 变量在一个 JS 文件的多个 HTML 文件中持续存在的问题

javascript - 将现有的 Bootstrap 侧边栏修改为可折叠

javascript - 处理 CSS 加载失败

javascript - 单选按钮的 jQuery 不起作用

html - 使用适当的分页符将 html/css 打印媒体显示转换为 .doc?