当我使用 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();
});
正在发送的参数是,
music=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();
}
}
我错过了什么吗?这是一个常见问题吗?
最佳答案
好吧,你的 PHP 中有一个拼写错误,这可能是你的服务器被阻塞的原因:echo json_endcode($data);
应该是echo json_encode($data);
。除此之外,您的 HTTP 服务器可能还存在其他问题。您使用什么服务器?一个好的做法是找到服务器错误日志和 PHP 错误日志并使用 tail -f
或其他一些监控日志的方法,当您遇到 505 错误时,这些方法应该会为您提供更多信息。
关于javascript - ajax请求全部返回错误500,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9745334/