Jquery ajax代码在$. done(function())之后没有被执行
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",
});
}
谢谢你,我将非常感谢社区在这个问题上的帮助:)
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()不会被执行。
相关文章:
- 如何在`window.open`之后执行回调
- 在$state.go之后执行$window.location.reload(true)
- Javascript:在每个onmouseover事件之后执行2个函数
- 可以使 jquery 点击事件在所有其他点击事件之后执行
- 在两个元素的 onrender 事件之后执行函数
- 流星技术/模式,用于等待数据库变量更改,然后在 in 之后执行某些操作
- 在jQuery .load Ajax之后执行if-statement
- 使javascript在firefox中的插件javascript之后执行
- 在catch之后执行
- 是否可以在特定的console.log()条目之后执行Javascript函数
- oncomplete 方法并不总是在操作 JSF 之后执行
- nodejs:setImmediate 回调在 setTimeout(fn, 0) 之后执行,与 nodejs 文档描述
- 为什么我不能在我的OnclientClickEvent之后执行我的onClick
- 在 ajax 之后执行数据表或重新绘制表
- 在 jQuery / Javascript 中的 bind() 事件之后执行条件
- 如何使 jQuery 语句在 .load() 之后执行
- 在具有 FS 操作的 FOR 循环之后执行
- 在 dgrid renderRow 之后执行一个函数
- window.print() 之后的 Javascript 有时在打印对话框之前执行,有时在打印对话框之后执行
- 在 3SE 之后执行函数