使用ajax将数据发送到php - 内部服务器错误(500)

时间:2012-03-25 10:16:32

标签: php ajax

我尝试用ajax将我的数据发送到php,但是有一个奇怪的错误。

这是我的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。例如,当我尝试将$ data发送到ajax时, 我以json模式打印我的ID:

enter image description here

但是,当我尝试将$ artistIds发送到ajax端时,我会给出500错误原因?

2 个答案:

答案 0 :(得分:3)

Selam:)

应该是:

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

    $this->sendJSONResponse($artistIds);
}

看看json_decode()。如果你想将它用作数组,你必须将第二个参数设置为 true ,否则使用$data->{'artistIds'};:)

答案 1 :(得分: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);
}
相关问题