Reputation: 966
I'm trying to reach a PHP-File via AJAX.
When I'm using a simple PHP-File like this:
<?php
header('Content-Type: text/html; charset=utf-8');
header('Cache-Control: must-revalidate, pre-check=0, no-store, no-cache, max-age=0, post-check=0');
$mode = $_POST['ajax_mode'];
switch ($mode) {
case "test":
if (isset($_POST['...'])) {
$test = $_POST['...'];
echo $test;
}
break;
}
?>
It's working fine. But if I try to use other classes, like a sql-class im getting a internal Server Error.
Here is my AJAX-Script:
function ajax(test, test1)
{
if (typeof test!== "undefined")
{
test_set = true;
if (window.XMLHttpRequest) {
request = new XMLHttpRequest();
} else if (window.ActiveXObject) {
try {
request = new ActiveXObject('Msxml2.XMLHTTP');
} catch (e) {
try {
request = new ActiveXObject('Microsoft.XMLHTTP');
} catch (e) {
}
}
}
if (!request) {
alert("No Chance!");
return false;
} else {
var url = ".../.../.../ajax_responder.php";
request.open('POST', url, true);
request.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
request.send('ajax_mode=test' + '&test=' + test);
request.onreadystatechange = function() {
if (request.readyState === 4)
{
interpretRequest();
}
};
}
}
}
function interpretRequest() {
if (request.status != 200) {
alert("Request-Error: " + request.statusText);
} else {
var content = request.responseText;
var string_collection = content.split("_");
alert(string_collection[0]);
}
}
});
EDIT: Here is the server-side script with the use of classes, with this version i get the Internal Server Errors
<?php
include '../../sql.php';
header('Content-Type: text/html; charset=utf-8');
header('Cache-Control: must-revalidate, pre-check=0, no-store, no-cache, max-age=0, post-check=0');
$mode = $_POST['ajax_mode'];
switch ($mode) {
case "test":
if (isset($_POST['...'])) {
$ace = $_POST['...'];
$sql = new sql_();
echo $sql->execute();
}
break;
}
?>
Upvotes: 0
Views: 157
Reputation: 966
I found an answer :D
the sql-class that i uncluded into my ajax responder calls another class, but the class is not known because i dont included the file of that class.
I included the new file and everything works just as fine :)
Upvotes: 0
Reputation: 4481
my suggestion and possible answer - place your include
after your headers:
<?php
header('Content-Type: text/html; charset=utf-8');
header('Cache-Control: must-revalidate, pre-check=0, no-store, no-cache, max-age=0, post-check=0');
include '../../sql.php';
$mode = $_POST['ajax_mode'];
switch ($mode) {
case "test":
if (isset($_POST['...'])) {
$ace = $_POST['...'];
$sql = new sql_();
echo $sql->execute();
}
break;
}
?>
it could cause problems if you first include a file (which possibly produces error-messages) and then send your headers. as a rule of thumb, headers in PHP
should be always set before any output occurs or could occur (unless you are using output-buffering, but that's another story).
if this doesn't help - have you tried to completely leave out your headers already?
Upvotes: 1