2012-03-25 136 views
0

我嘗試用ajax發送我的數據到php,但有一個奇怪的錯誤。用ajax發送數據到php - 內部服務器錯誤(500)

這是我的AJAX腳本,

function deleteData2() 
{ 
    var artistIds = new Array(); 

    $(".p16 input:checked").each(function(){ 
     artistIds.push($(this).attr('id')); 
    }); 


$.post('/json/crewonly/deleteDataAjax2', 
     { json: JSON.stringify({'artistIds': artistIds}) }, 
     function(response){ 
     alert(response); 
}); 


} 

我覺得這工作正常,但在PHP的一面,我面對500內部服務器錯誤(500)。

public function deleteDataAjax2() { 

      $json = $_POST['json']; 
      $data = json_decode($json); 
      $artistIds = $data['artistIds']; 

       $this->sendJSONResponse($artistIds); 
    } 

上面的代碼是我的PHP。例如,當我嘗試$數據發送到阿賈克斯, 打印我的ID在JSON模式:

enter image description here

然而,當我嘗試發送$ artistIds到阿賈克斯,我給了500錯誤,爲什麼?

+2

檢查您的Web服務器錯誤日誌,它會給你,爲什麼你得到500的更多信息錯誤。 – 2012-03-25 10:19:04

+0

注意:未定義的索引:json why:S – user1277467 2012-03-25 10:33:20

+0

嘗試'$ _REQUEST ['json']' – Dutchie432 2012-03-25 10:40:06

回答

3

塞拉姆:)

正確的應該是:

public function deleteDataAjax2() { 
    $json = $_POST['json']; 
    $data = json_decode($json, true); 
    $artistIds = $data['artistIds']; 

    $this->sendJSONResponse($artistIds); 
} 

json_decode()。如果你想用這個作爲一個數組,你必須在第二個參數設置爲,否則使用$data->{'artistIds'}; :)

+1

türküngücüeyw:D – user1277467 2012-03-25 12:06:50

+0

sorun deyil,ki sinemalar da bende ara sira geziyorum :) yenilenmesi iyi olur; D – ahmet2106 2012-03-26 15:52:18

0

嘗試這樣的事情,看看你是否得到迴應。

$.getJSON('/json/crewonly/deleteDataAjax2', 
     { 'artistIds' : artistIds }, 
     function(response){ 
     alert(response); 
}); 

public function deleteDataAjax2() { 

     $json = $_REQUEST['artistIds']; 
     $data = json_decode($json); 
     var_dump($data);die(null); 
} 
+0

:未定義索引:artistIds:D – user1277467 2012-03-25 10:50:35