I am attempting to figure out this error I have been getting tonight with a php script I am working on. So far this is the only one of my scripts, all set up in the same fashion, that is generating a failure on an Ajax POST request where a JSON string is returned to the html page for further processing.
First, here is the php script:
<?PHP
header('Content-Type: application/json');
include_once 'functions.php';
/*if(empty($_POST['uname']))
{
$data = array("error" => "un_empty");
echo json_encode($data);
exit();
}
if (empty($_POST['pword']))
{
$data = array("error" => "pw_empty");
echo json_encode($data);
exit();
}
if (empty($_POST['pword2']))
{
$data = array("error_2" => "pw2_empty");
echo json_encode($data);
exit();
}
if ($_POST['pword'] <> $_POST['pword2'])
{
$data = array("error" => "pw_nm");
echo json_encode($data);
exit();
}
if (empty($_POST['em']))
{
$data = array("error" => "em_empty");
echo json_encode($data);
exit();
}
if (empty($_POST['em2']))
{
$data = array("error" => "em2_empty");
echo json_encode($data);
exit();
}
if ($_POST['em'] <> $_POST['em2'])
{
$data = array("error" => "em_nm");
echo json_encode($data);
exit();
}
$username = $_POST['uname'];
$password = $_POST['pword'];
$email = $_POST['em'];*/
$username = "TestUser";
$password = "maS_t!@";
$email = "geowil11@gmail.com";
//echo $username."|".$email."\n";
$returnedData = register($username,$password,$email);
if ($returnedData === "true")
{
$returnedData = "noerror";
$data = array("success" => $returnedData);
echo json_encode($data);
//var_dump($data);
exit();
}
else
{
$data = array("error" => $returnedData);
echo json_encode($data);
//var_dump($data);
exit();
}
?>
Using var_dump on the "else" json return generates an array with the following elements: "error" => "exists1"
Which is what is expected as the account information being passed to the registration function already exists in my database.
Here is the actual AJax call from the html file:
var dataStr = $('#reg').serialize();
alert(dataStr);
$.ajax(
{
url: "php/register.php",
type: "post",
data: dataStr,
success: function(jsonstr)
{
onSuccess(jsonstr);
},
error: function(XMLHttpRequest, textStatus, errorThrown)
{
alert("Status: " + textStatus);
alert("Error: " + errorThrown);
}
});
});
});
The serialized object is a form that normally passes data to the registration.php file which I added above but for now I have the checks and setting of the POST data to the php vars used commented out for testing.
I have tried a variety of things to try and fix the issue including messing around with the form and the data being returned to the html page. So far all I have gotten back is JSON.parse yelling at me about unexpected characters.
Edit:
For clarity here are the exact errors:
Status: parsererror
Error: SyntaxError: JSON.parse: unexpected character
And for further problem solving, here is an alert of an example serialization of the form on the HTML page:
uname=TestUser&pword=12qwE!&pword2=12qwE!&em=geowil11%40gmail.com&em2=geowil11%40gmail.com