0

Simple request being made

    var username = {'username' : 'tom'};

    var request = $.ajax({
        url      : 'test.php',
        method   : 'GET',
        data     : JSON.stringify(username),
        dataType : 'json'
    });

    request.done(function(response) {
        response = JSON.parse(response);
        console.log(response);
    });

    request.fail(function(xhr, status, error) {
        console.log(error);
    });

PHP:

<?php
echo json_encode(array("bar" => "bar"));
?>

Still getting error, no idea why

enter image description here

enter image description here

enter image description here

3
  • OK I think I'm on to something Commented Mar 23, 2018 at 0:27
  • nevermind no luck yet Commented Mar 23, 2018 at 0:34
  • Simplified the code Commented Mar 23, 2018 at 3:23

1 Answer 1

4

that's because the server is returning a non valid JSON string. Try to check what the server is returning. This may happen if your server is throwing an error. In your case, I think the error you are trying to parse is not a JSON string. You might want to check that out as well.
You can use this link to validate your JSON.

Sign up to request clarification or add additional context in comments.

8 Comments

Well here is your problem, What is the value of the error variable ?
I commented out the server code. I believe the problem lies around data : JSON.stringify({username : username}),
When I change dataType: json to html it works. The weird thing is the server is not even giving a response, because I commented the code out for debugging but I'm still getting the error.
I think it's the JSON.parse(error) try commenting that out.
Commented it out but am still not getting a response from the server and I uncommented the server code.
|

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.