ajax请求所有返回错误500

时间:2012-03-16 22:32:09

标签: javascript jquery ajax post

当我用jquery做一个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();
});

正在发送的参数是,

  

音乐= Savage Garden& isAjax = 1

ajax请求的PHP方法看起来像这样,

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 个答案:

答案 0 :(得分:2)

好的,你的PHP中有一个拼写错误,可能是你的服务器窒息:echo json_endcode($data);应该是echo json_encode($data);。除此之外,您的HTTP服务器可能还有其他问题。你用的是什么服务器?一个好的做法是找到服务器错误日志和PHP错误日志,并使用tail -f或其他一些监视日志的方法,这些方法可以在您拥有505s时提供更多信息。