问题
Issue
I'm receiving the same error (500 Internal Server error) for all AJAX calls on a site I'm developing on my localhost (WAMP). I'm beginning to think that it might be the way my server is set up. Maybe I'm missing a PHP extension or something? Any help would be much appreciated.
Testing
I know the code works fine because i've navigated to the ajax action on my browser. Also the code actually saves data to the database so I know it's finding the correct script and running it ok.
Code
I've simplified the code to make testing easier.
JS
$.ajax({type: 'GET',
url: '/cms/performances/deleteperformance',
data: 'id=' + id,
dataType: 'json',
success: function(result)
{
switch(result)
{
case 1:
//Refresh the page
location.reload();
break;
case 0:
alert('error');
break;
case 2:
alert('Incorrect call');
break;
default:
alert('default');
break;
}
},
error: function(xhr, ajaxOptions, thrownError)
{
var httpCode = xhr.status;
alert(httpCode + ': ' + thrownError);
}
});
PHP
public function deleteperformanceAction()
{
$this->_helper->layout()->disableLayout();
$this->_helper->viewRenderer->setNoRender(true);
echo json_encode(1);
}
回答1:
Add this code at the top of your PHP:
ini_set('display_errors', 1);
error_reporting(E_ALL);
That will show you what the problem is in your PHP code. If it's a syntax error (seems quite likely for a code 500), then you'll need to either enable these options in your php.ini
, httpd.conf
or .htaccess
files. Alternatively, just take a look in your web server error log for the issue.
A 500 error is not a problem with your JS - it's server-side.
回答2:
I've finally got to the bottom of this. In fact there was nothing wrong with the code. However, I did have a plugin that wasn't being found during bootstrapping. This was then being run through my error handler which was changing the header to a 500. Once I'd removed the unfound plugin everything was fine.
Stupid mistake to make I suppose!
来源:https://stackoverflow.com/questions/10065898/500-internal-server-error-all-ajax-calls-php