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条)

报告相同问题?