Reputation: 5859
Im getting an error 200 in my ajax request. Im wanting to post 1 value which is a email address to the page in my ajax request. Could someone tell me what is wrong
Error:
message=:[object Object], text status=:parsererror, error thrown:=SyntaxError: JSON.parse: unexpected end of data
JQuery
$('#checkemail').click(function() {
$.ajax({
url:'http://' + location.host + '/buyme/include/getemailaddress.php',
type:'POST',
contentType: "application/json; charset=utf-8",
data: {email:$('#email').val()},
dataType:"json",
success: function(msg) {
alert(msg);
},
error: function(ms, textStatus, errorThrown) {
alert(errorThrown);
}
});/**/
});
Upvotes: 0
Views: 931
Reputation: 10348
As you are using json data type any returned data from server must be in that format.
So FIRST Don't forget that you send post
data, so work with:
if ($_SERVER['REQUEST_METHOD'] == 'POST')
{
$email = $_POST['email'];
// ...
// and SECOND return suitable data type, ie, a json coded string.
echo json_encode($your_result);
// where $your_result can be simple as
// $your_result['result'] = true;
// $your_result['result'] = false;
// a message
// $your_result['msg'] = 'all OK';
// a message and and a flag
// $your_result['msg'] = 'all OK';
// $your_result['result'] = true;
}
So in your jquery callback you get returned data like this:
success: function(data) {
if (data.msg != "") alert(data.msg);
if (data.result === true) {} else {}
},
Upvotes: 1