【问题标题】:Jquery ajax code in .done(function()) after $.when() not being executed未执行 $.when() 后 .done(function()) 中的 Jquery ajax 代码
【发布时间】:2014-05-28 11:04:18
【问题描述】:

这是问题所在的 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方法超时后删除逗号:30000,

标签: javascript jquery ajax


【解决方案1】:

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

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

sendImageData(imageJSON)

返回你的错误。因此,您的 .done() 没有被执行。

【讨论】:

  • 您好,谢谢。那是一种非常愚蠢的调试方式。无论如何,我已取消注释 url 并完全删除了超时部分。该动作现在似乎正在执行。但是,当我调试我的操作时,似乎 JSON 没有出现在 $_POST 中。但它显示在我的 get_files_content(php://input) 中。如何在 $_POST 中获取 json 以便将成功设置为 ajax?
  • Json自带jQuery ajax的succss函数
  • 是的,但是 if 语句 jqXHR.readyState==4 && jqXHR.status==200 甚至没有执行,我认为这是因为 $_POST 在我的动作控制器中是空的,因此由于某种原因action 永远不会返回 ajax 已经完成?
猜你喜欢
  • 1970-01-01
  • 2016-09-25
  • 1970-01-01
  • 1970-01-01
  • 2016-04-20
  • 2014-08-14
  • 2023-04-08
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多