簡體   English   中英

AJAX無法正確發送FormData嗎?

[英]AJAX does not send FormData Correctly?

我正在研究將一個圖像文件和少量數據發送到PHP文件的方法。 所以我正在使用表單來執行此操作。 這是我的JS代碼;

var pictureForm = new FormData();
var imgFile = $('#avatarSelect')[0];
pictureForm.append('email', email);
pictureForm.append('firName',editfirName);
pictureForm.append('lstName',editlstName);
pictureForm.append('newPass',editpass);
pictureForm.append('newPhn', editphnNum);
pictureForm.append('pictureFile', imgFile.files[0]);

$.ajax({
        type: "POST",
        url: "php/accountUpdate.php",
        dataType:'json',
        data:{pictureForm},
        processData: false,
        contentType: false,
        success : function(response)
        {
            if(response==1)
            {
                alert("Success");

            }
            else
            {
                alert(response);

            }
        }
    });

和accountUpdate.php;

<?php
if($_POST)
{
$userEmail= $_POST['email'];
$userPass= $_POST['newPass'];
$userPhone = $_POST['newPhn'];
$userFName= $_POST['firName'];
$userLName = $_POST['lstName'];
$servername ="localhost";
$username="root";
$password="";
$dbname="AS2014459";

// Some code
}
else
 echo json_encode("Error");
?>

我的問題

但是,這總是提醒我“錯誤”。 表示$ _POST設置不正確。 我找不到原因。 但是,所有變量(例如email,editfirname ... etc)均已正確設置並具有值。

另外,當我在本地服務器上運行該命令(刪除PHP if($ _ POST)條件)時,控制台日志顯示未定義的索引:電子郵件。 有什么猜想嗎?

您尚未關閉if / else語句,因此它每次都會回顯錯誤,並使用isset($ _ POST)

嘗試使用布爾值( true )更改1

var pictureForm = new FormData();
var imgFile = $('#avatarSelect')[0];
pictureForm.append('email', email);
pictureForm.append('firName',editfirName);
pictureForm.append('lstName',editlstName);
pictureForm.append('newPass',editpass);
pictureForm.append('newPhn', editphnNum);
pictureForm.append('pictureFile', imgFile.files[0]);

$.ajax({
        type: "POST",
        url: "php/accountUpdate.php",
        dataType:'json',
        data:{pictureForm},
        processData: false,
        contentType: false,
        success : function(response)
        {
            if(response==true)
            {
                alert("Success");

            }
            else
            {
                alert(response);

            }
        }
    });

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM