Jquery ajax代码在$. done(function())之后没有被执行

Jquery ajax code in .done(function()) after $.when() not being executed

本文关键字:之后 执行 ajax function done Jquery 代码      更新时间:2023-09-26

这是问题所在的jquery代码。我想让ajax发送json数据到服务器,然后提交表单。如果我没有when和done子句,那么它是可能的提交是在ajax之前完成的,将无法及时检索成功或错误。

 function deleteImage(button)
    {
        //There is only one image that is a sibling of the delete button
        var image = $(button).siblings(".MultiFile-image")[0];
        var groupId = $(image).data("modelId");
        var imgId = $(image).data("id");
        var imgSrc = $(image).attr("src");
    //Delete the image view after the removed button is clicked but the data to be sent to the server for deletion is already stored
    $(button).parent(".MultiFile-label").remove();

    var imageToDelete = {imgId:imgId, imgSrc:imgSrc, groupId:groupId};
    var imageJSON =  '{"imageToDelete":' + JSON.stringify(imageToDelete) + "}";
    //This is needed to check whether ajax has been executed before submission
    var sentImageData = false;
    $("form").submit(function(e) {
        //Stop submission, need to send data through ajax first, will submit after ajax is executed later.
        if(!sentImageData)
        {
            e.preventDefault();
    //Send the images for deletion only when the form has been submitted
    //For some reason this code is never executed and go immediately to the end of this method
 $.when(sendImageData(imageJSON)).done(function(jqXHR) {
                if(jqXHR.readyState == 4 && jqXHR.status == 200)
                {
                    sentImageData = true;
                    $("form").submit();
                }
                else
                {
                    console.log(jqXHR);
                    sentImageData = false;
                }
            }); //For some reason the debugger skips to here and return is undefined
            }
            //If executed is true, send the form as normal
        });
}
/**
 * @var imageJSON the image json data that will be sent to the server to delete the image
 * @returns {@exp;$@call;ajax} return XMLHttpRequest of the ajax
 */
function sendImageData(imageJSON)
{
   return $.ajax({
            type: 'POST',
            data: imageJSON,
            dataType: 'JSON',
            url: "index.php?r=artworkGroup/deleteArtwork",
        });
}

谢谢你,我将非常感谢社区在这个问题上的帮助:)

EDIT:下面是处理这个ajax代码的操作。json的一个示例是:" {"imageToDelete":{"imgId":2,"imgSrc":"upload_file/artwork/1-New_Artwork_Group/12861274.jpg","groupId":2}}"

  public function actionDeleteArtwork() {
            $noError = false;           
            if(isset($_POST["imageToDelete"]))
            {
                $imageArray = $_POST["imageToDelete"];
                //Delete every image retrieved by post
                foreach($imageArray as $image)
                {
                    $transaction = Yii::app()->db->beginTransaction();
                    try{
                        $imageToDelete = json_decode($image);
                        $model = $this->loadModel($imageToDelete->groupId);
                        $artworkToDelete = $model->artworks->loadModel($imageToDelete->id);
                        if($imageToDelete->imgSrc == $artworkToDelete->imgSrc)
                        {
                            $artworkToDelete->delete();                    
                            if(file_exists($imageToDelete->imgSrc))
                            {
                                unlink($imgToDelete->imgSrc);
                            }
                        }    
                        else
                        {
                            $hasError = true;
                        }   
                        $transaction->commit();
                    }
                    catch(Exception $e)
                    {
                        $transaction->rollback();
                        $hasError = true;
                    }
                    //Delete the image files if there are no errors and that the file exists, otherwise just ignore 
                    if(file_exists($imageToDelete->imgSrc) && $noError)
                    {
                        unlink($imageToDelete->imgSrc);
                    }
                }
            }
       }

您在ajax请求中省略了url。这意味着它将击中当前页面的url。这可能会触发超时。

超时是$.ajax中的一种错误。这就是为什么你的

sendImageData (imageJSON)

返回false。因此,你的。done()不会被执行。