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.

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

the error function on $.ajax is called when there was a problem with the request. However, you are using the expected results of the script running successful, but returning an error:

error: function(response) {
    alert("Error: " + response.ErrorMessage);
}

response.ErrorMessage will not be set (only in the success callback), because that's something you return when the request was successful, although the logic inside the request may return an error, but that'sw hat you are handeling in the success callback.

This is the way to catch an erroneous call:

error: function(xhr, status, error) {
    alert("Error: " + status + ' ' + error);
}

as noted in the comments, this line is also faulty:

objUploadImage.UserID = <?php echo $_SESSION['UserId']; ?>;
                                                needs a ; ^

if the UserId is not an integer ID, it does need quotes.

Inspect that you are getting the file in $_FILES["file"] array or not

I think its the jquery's old method "handleError" which has been removed from the jQuery since version 1.5, you can use this custom error handler inside your javascript, anywhere are jQuery:

jQuery.extend({
handleError: function( s, xhr, status, e ) {
    // If a local callback was specified, fire it
    if ( s.error )
        s.error( xhr, status, e );
    //Log it in the console
    else if(xhr.responseText)
        console.log(xhr.responseText);
}});