dongliulu1122 2015-02-07 14:28
浏览 32
已采纳

使用ajax-php发送变量时出错

I try make a login using ajax, but when sending the variables to a php file arriv empty..

ajax:

  function enviar()
  {
    var ced=$("#cedula").val();
    var con=$("#contraseña").val();
    $.ajax({
      async:true,
      type: "POST",
      dataType: "html",
      contentType: "application/x-www-form-urlencoded",
      url:"sesion/index.php",
      data:{"cedula": ced,"contraseña": con},
      beforeSend:inicioEnvio,
      success:llegada,
      error:problemas
    }); 
      return false;
  }

php code:

 $operaciones=new operaciones();
    $operaciones->conexion();
    if($operaciones->verificar_login('usuario','USU_Cedula',
       'USU_Contraseña',$_POST['cedula'],
        $_POST['contraseña'], $result)== 1){codigo}
  • 写回答

2条回答 默认 最新

  • duanli9930 2015-02-07 14:32
    关注

    In PHP, try the following code... it should give you a dump off everything sent..

    <?php
      echo "Request:<br /> ", print_r($_REQUEST,true), "<br />";
      echo "Get:<br /> ", print_r($_GET,true), "<br />";
      echo "Post:<br /> ", print_r($_POST,true), "<br />";
    ?>
    

    Otherwise, the js looks fine at a glance. i would suggest the POST method of jquery though.

    $.post( "sesion/index.php", { "cedula": ced,"contraseña": con }).done(function( data ) {
        alert( "Data Loaded: " + data );
    });
    

    Using post saves having to deal with some of the finer settings and should work in general use. PHP seems to play nicely with it as well..

    The before event can be just a call in front of the post command and there is a error function as well that can be used.

    EDIT

    Sorry, miss read it i think. By the looks of it, your not getting a value from your js vars..

    Try doing an alert straight after you obtain their value and try switching them in the post... try sending test strings to your PHP.

    var ced=$("#cedula").val();
    var con=$("#contraseña").val();
    alert(ced);
    alert(con);
    
    /* Try this in your post */
    data:{"cedula": "test1","contraseña": "test2"},
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥50 三种调度算法报错 有实例
  • ¥15 关于#python#的问题,请各位专家解答!
  • ¥200 询问:python实现大地主题正反算的程序设计,有偿
  • ¥15 smptlib使用465端口发送邮件失败
  • ¥200 总是报错,能帮助用python实现程序实现高斯正反算吗?有偿
  • ¥15 对于squad数据集的基于bert模型的微调
  • ¥15 为什么我运行这个网络会出现以下报错?CRNN神经网络
  • ¥20 steam下载游戏占用内存
  • ¥15 CST保存项目时失败
  • ¥20 java在应用程序里获取不到扬声器设备