$ _POST无法以ajax形式提交?

时间:2016-06-07 07:00:39

标签: javascript php json ajax

当我尝试检查用户输入名称是否已经由ajax表单提交时存在!但它只在Undefined index: username中得到sessions.php,缺少什么?

<form action="" method="POST" id="saveuser" enctype="multipart/form-data">
<input type="text" name="username"><br>
<input type="password" name="pass"><br>
<input type="file" name="fileupload"><br>
<input type="submit" name="submit" value="Confirm" id="confirm">
</form>
<script type="text/javascript">
    $('#confirm').click(function(e){
        e.preventDefault();
      $.ajax({
            type:'POST',
            url :"sessions.php",
            data:$("#saveuser").serialize(),
            contentType : false,
            processData: false,            
            success: function(d){
                console.log(d);//[error] :Undefined index: username 
            }
        });
    });
</script>

sessions.php

<?php
$exist = "david";
if($_POST['username'] == $exist){
    echo json_encode("Already exist");
}
else{
    echo json_encode("You can succesfully add");
}
?>

11 个答案:

答案 0 :(得分:12)

如果您将 contentType 设置为 false ,则不会发送ajax标头,如果您发送somehing type:POST标头,则结果不包含您的数据,因此服务器看不到它。如果您使用GET来执行此操作,则会有效,因为数据是使用GET(在网址之后)不在标头中发送的。

只需删除 contentType

即可
    $.ajax({
            type:'POST',
            url :"sessions.php",
            data: $("#saveuser").serialize(),
            success: function(d){
                console.log(d);
            }
    });
  

contentType

     

(默认:'application / x-www-form-urlencoded;   charset = UTF-8')

     

类型:布尔值或字符串向数据发送时   服务器,使用此内容类型。默认是   “application / x-www-form-urlencoded; charset = UTF-8”,这很好   大多数情况下。如果您明确地将内容类型传递给$ .ajax(),那么   它总是被发送到服务器(即使没有数据发送)。作为   jQuery 1.6你可以传递 false 来告诉jQuery不要设置任何内容   类型标题。注意:W3C XMLHttpRequest规范规定了这一点   charset总是UTF-8;指定另一个字符集不会强制   浏览器更改编码。注意:对于跨域请求,   将内容类型设置为除以外的任何内容   application / x-www-form-urlencoded,multipart / form-data或text / plain   将触发浏览器发送预检OPTIONS请求   服务器

processData用于按原样发送数据 - Ajax documentation

  

将数据发送到服务器

     

默认情况下,使用GET HTTP方法发送Ajax请求。如果   需要POST方法,可以通过设置a来指定方法   type选项的值。此选项会影响内容的方式   数据选项被发送到服务器。 POST数据永远是   根据W3C,使用UTF-8字符集传输到服务器   XMLHTTPRequest标准。

     

data选项可以包含表单的查询字符串   key1 = value1&amp; key2 = value2,或{key1:'value1'形式的对象,   key2:'value2'}。如果使用后一种形式,则转换数据   在发送之前使用jQuery.param()将查询字符串转换为查询字符串。这个   可以通过将processData设置为 false 来规避处理。该   如果您希望发送XML对象,则可能不希望进行处理   服务器;在这种情况下,请更改contentType选项   application / x-www-form-urlencoded为更合适的MIME类型。

答案 1 :(得分:8)

您的代码存在一些问题,例如:

  •   

    ...它只获得未定义的索引:sessions.php中的用户名

    问题在于以下两行,

    contentType : false,
    processData: false,
    

    来自the documentation

      

    contentType (默认:'application/x-www-form-urlencoded; charset=UTF-8'
      输入:BooleanString
      将数据发送到服务器时,请使用此内容类型。默认为“application / x-www-form-urlencoded; charset = UTF-8”,这在大多数情况下都适用。如果您明确地将内容类型传递给$.ajax(),则它始终会发送到服务器(即使没有数据发送)。从jQuery 1.6开始,您可以传递false来告诉jQuery不要设置任何内容类型标头。

      

    processData (默认:true
      输入:Boolean
      默认情况下,作为对象传入数据选项的数据(技术上,除了字符串之外的任何东西)将被处理并转换为查询字符串,适合默认的内容类型“application / x-www-form -urlencoded“即可。如果要发送DOMDocument或其他未处理的数据,请将此选项设置为false

    因此,如果您将$_POSTcontentType设置为processDatafalse数组在 sessions.php 页面中将为空,这就是原因你得到这个未定义索引:用户名错误。但话虽如此,由于您发送的文件包含您的AJAX请求,因此可以将这些设置设置为false,这将在下面进一步说明。

  • .serialize()方法通过序列化表单控件值来创建URL编码的文本字符串,例如<input><textarea><select>。但是,在序列化表单时它不包含文件输入字段,因此远程AJAX处理程序根本不会接收文件。因此,如果您通过AJAX上传文件,请使用FormData对象。但请记住,旧浏览器不支持FormData对象。 FormData支持从以下桌面浏览器版本开始:IE 10 +,Firefox 4.0 +,Chrome 7 +,Safari 5 +,Opera 12 +。

  • 由于您期望服务器提供json对象,因此请将此设置dataType:'json'添加到您的AJAX请求中。 dataType是您期望从服务器返回的数据类型。

所以解决方案就是这样:

保持 HTML 表单不变,并按以下方式更改 jQuery / AJAX 脚本,

$('#confirm').click(function(e){
    e.preventDefault();

    var formData = new FormData($('form')[0]);
    $.ajax({
        type: 'POST',
        url : 'sessions.php',
        data: formData,
        dataType: 'json',
        contentType: false,
        processData: false,            
        success: function(d){
            console.log(d.message);
        }
    });
});

sessions.php 页面上,按照以下方式处理您的表单:

<?php

    $exist = "david";
    if(isset($_POST['username']) && !empty($_POST['username']) && isset($_POST['pass']) && !empty($_POST['pass'])){
        if($_POST['username'] == $exist){
            echo json_encode(array("message" => "Already exist"));
        }else{
            echo json_encode(array("message" => "You can succesfully add"));

            // get username and password
            $username = $_POST['username'];
            $password = $_POST['pass'];

            // process file input
            // Check whether user has uploaded any file or not
            if(is_uploaded_file($_FILES['fileupload']['tmp_name'])){

                // user has uploaded a file

            }else{
                // no file has been uploaded
            }
        }
    }else{
        echo json_encode(array("message" => "Invalid form inputs"));
    }

?>

答案 2 :(得分:4)

您将contentType设置为false,这就是PHP无法解析您的帖子主体

的原因

答案 3 :(得分:3)

对ajax使用$ .post():

$('#confirm').click(function(e){

    e.preventDefault();

    $.post("sessions.php", $.param($("#saveuser").serializeArray()), function(data) { // use this ajax code
       console.log(data);
    });

});

答案 4 :(得分:3)

在html代码中使用以下代码并删除 contentType:false,             processData:false

$mdMenuIsOpen

答案 5 :(得分:3)

考虑到这是您的HTML表单

<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.0.0-rc1/jquery.min.js"></script>
    jQuery("#saveuser").submit(function () {
    
    		//Validate the input here
    
    		jQuery.ajax({
    			type: 'POST',
    			url: 'sessions.php',
    			data: jQuery('#saveuser').serialize(),
    
    			success: function (msg) {
    				msg = jQuery.trim(msg);
    				if (msg == 'Success') {
                       //Do Whatever					
                       //jQuery("#thanks_message").show('slow');
    				}
    			}
    		});
    		return false;
    	});

您可以像这样调用jQuery函数

<?php

  $username = trim($_POST['username']);
  $pass = trim($_POST['pass']);
  //rest of the params of the form

  $exist = "david";
  if ($username == $exist) {
    echo json_encode("Already exist");
  } else {
    echo json_encode("You can succesfully add");
  }
?>

您将获得session.php文件中的所有参数,如

// open serial port
/*  O_RDWR means that the port is opened for both reading and writing
 *  O_NOCTTY means that no terminal will control the process opening the serial port
 */
fd = open(MODEMDEVICE, O_RDWR | O_NOCTTY ); // /dev/ttyS1
if (fd <0)
    perror(MODEMDEVICE);

tcgetattr(fd,&SerialPortSettings); /* save current serial port settings */

/* Set Baud Rate */
cfsetospeed (&SerialPortSettings, (speed_t)B230400);
cfsetispeed (&SerialPortSettings, (speed_t)B230400);

SerialPortSettings.c_iflag &= ~IGNBRK;         // disable break processing
SerialPortSettings.c_lflag = 0;                // no signaling chars, no echo, no canonical processing
SerialPortSettings.c_oflag = 0;                // no remapping, no delays

/* Setting other Port Stuff */
SerialPortSettings.c_cflag     &=  ~PARENB;              /* No Parity            */
SerialPortSettings.c_cflag     &=  ~CSTOPB;              /* Stop bits = 1         */
SerialPortSettings.c_cflag     &=  ~CSIZE;               /* Clears the Mask       */
SerialPortSettings.c_cflag     |=  CS8;                  /* Set the data bits = 8 */

SerialPortSettings.c_cflag     &=  ~CRTSCTS;           /* Turn off hardware based flow control */
SerialPortSettings.c_cc[VMIN]   =  0;                  // read doesn't block // specifies the minimum number of characters that should be read before the read() call returns
SerialPortSettings.c_cc[VTIME]  =  10;                 // 1 seconds read timeout
SerialPortSettings.c_cflag     |=  CREAD | CLOCAL;     /* Turn on the receiver of the serial port */
SerialPortSettings.c_iflag &= ~(IXON | IXOFF | IXANY); /* Turn off software based flow control  */


tcsetattr ( fd, TCSANOW, &SerialPortSettings ); //TCSANOW tells to make the changes now without waiting

我希望这可以解决你的问题。

答案 6 :(得分:2)

<form method="POST" id="saveuser" enctype="multipart/form-data">
<input type="text" name="username"/><br>
<input type="password" name="pass"/><br>
<input type="file" name="fileupload"/><br>
<input type="button" name="save" id="save" value="save"/>
</form>

<script type="text/javascript">
    $('#save').click(function(e){
      var form = new FormData(document.getElementById('#saveuser'));  

      $.ajax({

            url :"sessions.php",
            type : 'POST',
            dataType : 'text',
            data : form,

            processData : false,
            contentType : false,  
            success: function(d){
                console.log(d);//[error] :Undefined index: username 
            }
        });
    });
</script>

答案 7 :(得分:1)

您需要更改脚本:

尝试使用new FormData代替.serialize()

<script type="text/javascript">
    $('#confirm').click(function(e){
        e.preventDefault();
        var formData = new FormData($("#saveuser")[0]);
      $.ajax({
            type:'POST',
            url :"tt.php",
            data:formData,
            contentType : false,
            processData: false,            
            success: function(d){
                console.log(d);//[error] :Undefined index: username 
            }
        });
    });
</script>

注意:您使用contentTypefalse表示jQuery不添加Content-Type标头。您正在使用jQuery的.serialize()方法,该方法使用标准的URL编码表示法创建文本字符串。使用&#34; contentType:false&#34;。

时,需要传递未编码的数据

答案 8 :(得分:0)

将脚本更改为

<script type="text/javascript">
$(function(){
    $('#confirm').click(function(e){
        e.preventDefault();
      $.ajax({
            type:'POST',
            url :"sessions.php",
            data:$("#saveuser").serialize(),
            contentType : false,
            processData: false,            
            success: function(d){
                console.log(d);//[error] :Undefined index: username 
            }
        });
    });
});
</script>

答案 9 :(得分:0)

您的编码是正确的。从Ajax移除processData和contentType它将起作用

processData : false,
contentType : false,  

答案 10 :(得分:-5)

删除方法,操作:从表单标记发布和空白,因为您只需要在ajax方法中提供所有详细信息。

或者您可以删除表单标记本身,因为ajax方法将负责发布调用。

这将有希望解决

    <form id="saveuser" enctype="multipart/form-data">
相关问题