weixin_33711647 2013-09-11 07:26 采纳率: 0%
浏览 24

jQuery和Ajax中的错误

I write the code for uploading image in local machine in ajax and jquery. And if condition is not executed.

Here is the fnUploadImage script.

function fnUploadPhoto()
{
var image = $("#txtUploadImage").val();

var objUploadImage = new Object();
objUploadImage.ImageName = image;
objUploadImage.UserID = <?php echo $_SESSION['UserId']; ?>

$.ajax({
    type:"POST",
    url:"db.php?UploadImage",
    data: {data:objUploadImage},
    dataType:"json",
    async:false,
    success: function(response)
    {
        alert(response.ErrorMessage);
        if(response.IsError)
            $("#errorUploadPhoto")[0].innerHTML = response.ErrorMessage;
        else
            $("#errorUploadPhoto")[0].innerHTML = response.SuccessMessage;
    },
    error:function(response)
    {
        alert("Error: " + response.ErrorMessage);
    }
});
}

And this function calls the db.php UploadImage function which is as follows:

function UploadImage($data)
{
$allowedExts = array("gif", "jpeg", "jpg", "png");
$temp = explode(".", $_FILES["file"]["txtUploadImage"]);
$extension = end($temp);
if ((($_FILES["file"]["type"] == "image/gif")
|| ($_FILES["file"]["type"] == "image/jpeg")
|| ($_FILES["file"]["type"] == "image/jpg")
|| ($_FILES["file"]["type"] == "image/pjpeg")
|| ($_FILES["file"]["type"] == "image/x-png")
|| ($_FILES["file"]["type"] == "image/png"))
&& in_array($extension, $allowedExts))
    {
        if ($_FILES["file"]["error"] > 0)
        {
            die(json_encode(array("IsError" => true, "ErrorMessage" => "Error")));
            //echo "Return Code: " . $_FILES["file"]["error"] . "<br>";
            json_encode(array("IsError" => true, "ErrorMessage" => "Return Code: " . $_FILES["file"]["error"]));
        }
        else
        {
            if (file_exists("profile_images/" . $_FILES["file"]["txtUploadImage"]))
            {
                //echo $_FILES["file"]["name"] . " already exists. ";
                json_encode(array("IsError" => true, "ErrorMessage" => "Filename already exists."));
            }
            else
            {
                if($_FILES["file"]["size"]/1024 < 20)
                {
                    move_uploaded_file($_FILES["file"]["tmp_name"], "profile_images/" . $_FILES["file"]["txtUploadImage"]);
                    //echo "Stored in: " . "profile_images/" . $_FILES["file"]["name"];
                    json_encode(array("IsError" => false, "SuccessMessage" => "Image uploaded successfully."));
                }
                else
                {
                    json_encode(array("IsError" => true, "ErrorMessage" => "Minimum image size is 20 kb."));
                }
            }
        }
    }
else
{
    //echo "Invalid file.";
    json_encode(array("IsError" => true, "ErrorMessage" => "Invalid file."));
}
}

When i run this script it shows in me the Error called undefined. Please help me to solve this.

  • 写回答

4条回答 默认 最新

  • weixin_33736832 2013-09-11 07:32
    关注

    quotes are metter here. If you dont use codes it understands it is a variable which is undefined

    评论

报告相同问题?

悬赏问题

  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 对于相关问题的求解与代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 信号傅里叶变换在matlab上遇到的小问题请求帮助
  • ¥15 保护模式-系统加载-段寄存器
  • ¥15 电脑桌面设定一个区域禁止鼠标操作
  • ¥15 求NPF226060磁芯的详细资料