2012-03-16 198 views
0

當過我做的jQuery Ajax請求,我總是得到一個錯誤500的回報,Ajax請求都返回500錯誤

我張貼到以下網址

http://localhost/domain/index.php/my_profile/interests_music

使用此javascript,

$("#add").click(function(e){ 

    //set some process variables, we need to get the forms action, 
    //and any post data it is sending appending isAjax into the params 
    //gives us a point in the controller to gracefully check for ajax. 
    var action = $(this).parent('form').attr('action'); 
    var formData = $(this).parent('form').serialize()+"&isAjax=1"; 

    $.ajax({ 
     type: "POST", 
     url: action, 
     data: formData 
    }).done(function(msg) { 
     alert("Data Saved: " + msg); 
    }); 

    e.preventDefault(); 
}); 

該bems是bei吳派的,

音樂=野人花園& isAjax = 1

而且PHP方法的AJAX請求這個樣子的,

public function interests_music() 
{ 
    if($this->input->post('music')) 
    { 
     $this->rest->initialize(array('server' => 'https://www.googleapis.com/freebase/v1')); 
     $response = $this->rest->get('mqlread?query={"type":"/music/artist","name":"' . urlencode($this->input->post('music')) . '","id":[]}'); 
     $data['image'] = 'https://usercontent.googleapis.com/freebase/v1/image'.$response->result->id[0].'?mode=fillcrop&maxwidth=80&maxheight=80'; 
     $data['category'] = 'music'; 
     $data['user_id'] = $this->session->userdata('id'); 
     $data['name'] = $this->input->post('music', TRUE); 

     $this->profile_model->add_interest($data); 

     Events::trigger('interests_music'); 
     Events::trigger('badge_stagediver'); 

     if($this->input->post('isAjax') == 1) 
     { 
      echo json_endcode($data); 
      $this->_buttons(); 
     } 

     redirect('my_profile/interests'); 
    } 
    else 
    { 
     show_404(); 
    } 
} 

我缺少的東西,這是一個共同的問題?

+1

錯誤500意味着服務器端錯誤。嘗試尋找錯別字和語法錯誤。嘗試調試它,而不使用AJAX。另外,嘗試使用'json_encode()'而不是手動構建JSON響應 – Joseph 2012-03-16 22:35:33

+0

不應該使用.success回調代替.done – 2012-03-16 22:36:12

+0

@AmritpalSingh'.success()'將與'.error()'一起被棄用,並且'.complete()'。 http://api.jquery.com/jQuery.ajax/ – Joseph 2012-03-16 22:39:56

回答

2

對於其中之一,您的PHP中存在一個錯字,可能是您的服務器窒礙之處:echo json_endcode($data);應該是echo json_encode($data);。除此之外,您的HTTP服務器還可能存在其他問題。你使用什麼服務器?一個好的做法是找到服務器錯誤日誌和PHP錯誤日誌,並使用tail -f或其他一些監視日誌的方法,當你有505時應該給你更多的信息。

+0

+1中,OP也可以通過瀏覽器訪問他們的AJAX頁面,並且啓用了錯誤,屏幕。 – halfer 2012-03-16 22:41:51

+0

是的,非常真實,不知道爲什麼我首先想到了原木。 sico可能也可以在'done'處理程序中設置一個斷點並反省jqXHR對象或向請求添加成功和錯誤處理程序。 – tomswift 2012-03-16 23:40:51