0

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); 
        }   
    });/**/
});
3
  • 1
    Error 200? 200 is the code for success. From the error message I suspect you're not returning valid JSON Commented Feb 4, 2013 at 3:15
  • And what say getemailaddress.php? Commented Feb 4, 2013 at 3:16
  • show us your getemailaddress.php Commented Feb 4, 2013 at 3:17

1 Answer 1

1

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 {}
},
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.