I am trying to extract json response in jquery sent from php file. This is the .js code:
$.ajax({ url: 'index.php?page=register', //This is the current doc type: 'POST', datatype: 'json', data: {'userCheck': username}, success: function(data){ // Check if username is available or not }, error: function(){ alert('Much wrong, such sad'); } });
This is the response from the php file:
if($sth->fetchColumn()!=0){ //$response = array("taken"); $response = array("username"=>"taken"); echo json_encode($response); //echo '{"username':'taken"}'; }else{ //$response = array("available"); $response = array("username"=>"available"); echo json_encode($response); //echo '{"username":"available"}'; }
I tried all the combinations that I can think of in both files, but nothing works. This is a simple check for the username in the database. If I console the data received from the response, I get the following:
{"username":"available"}<!DOCTYPE html> // The rest of the page html
So, there is information, but how do I get it? I tried several syntaxes found on the Internet, but so far no luck. It seems I remember that the json response can only contain valid json, just like the html problem? I donโt think I can avoid this because of the structure of my application, so I hope you can access json with my current structure.
source share