2011-01-09 82 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函數來調試您的代碼中引發的任何錯誤。