簡體   English   中英

$ .when()之后未執行.done(function())中的jQuery ajax代碼

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

這是問題的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",
        });
}

謝謝,我非常感謝社區在此問題上的幫助:)

編輯:這是處理此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 這意味着它將點擊您當前的頁面網址。 那可能是觸發超時。

超時是$ .ajax中的一種錯誤。 那就是為什么你

sendImageData(imageJSON)

給你假的。 因此,您的.done()無法執行。

暫無
暫無

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

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