2011-01-09 80 views
0

我有这样的:jQuery的Ajax调用不会工作

$('.photoSaved').click(function() { 
    var savedPhoto = $(this).attr('id'); 
    $.ajax({ 
    url: "status.php", 
    type: "POST", 
    data: { mode: 'use', name: savedPhoto }, 
    success: function(){ 
     $.fancybox.close(); 
     $('#status_upContent').hide(); 
     $("#image").append($(document.createElement("img")).attr({src: "images/users/status/"+savedPhoto})).show(); 
    } 
    }); 
}); 

有:

if($_POST['mode'] && $_POST['mode'] == 'use'){ 
    $_SESSION['status_attachedImage'] = $_POST['name']; 
    echo 'ok'; 
} 

我可以看到它发出请求,但它不给我回应“OK”,并成功:不执行

+2

尝试添加`error`和`complete`处理程序来查看是否有错误。你也可以使用`$('img')。attr({src:“images/users/status /”+ savedPhoto})` – 2011-01-09 18:03:18

+0

你已经告诉我们它不会做什么,但是你没有告诉我们它做了什么! – Quentin 2011-01-09 18:04:55

回答

2

尝试添加“错误”回调。这会告诉你什么是错的。

错误(XMLHttpRequest参数url errorThrown)

A function to be called if the request fails. The function is passed three arguments: The XMLHttpRequest object, a string describing the type of error that occurred and an optional exception object, if one occurred. Possible values for the second argument (besides null) are "timeout", "error", "notmodified" and "parsererror". This is an Ajax Event.This handler is not called for JSONP requests, because they do not use an XMLHttpRequest.

所以在这条线,类似于您success回调补充:

error: function(XMLHttpRequest, textStatus, errorThrown) { 
    console.error(errorThrown); 
} 
1

如果您在Firefox或Safari开发,你可以使用console.error函数来调试您的代码中引发的任何错误。