Extract Json answer

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.

+5
source share
3 answers

in you ajax

EDIT:

change

 datatype:"json", 

the case of the parameter name was not respected, t must be T

 datatype:"json", 

now try again

 $.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 switch(data.username) { case "available": // do you want break; case "taken": // do you want break; } }, error: function() { alert('Much wrong, such sad'); } }); 

in php

just that, and don't forget to go out; to avoid including the html page in your json answer! This is the code following "..." that breaks your json output and make it unreadable javascript (worste, it will just break your javascript!)

 echo json_encode(["username"=> ($sth->fetchColumn()!=0) ? "taken":"available"]); exit; 
+2
source

When you answer an AJAX call, you should just return a JSON response, not an HTML page. Add:

 exit(); 

after this code so that you don't display HTML after JSON.

In the JS code, use if (data.username == 'available') to find out if the username is available.

Another problem in your code is that you have a typo:

 datatype: 'json', 

It should be dataType , with an uppercase T

You can also put:

 header("Content-type: application/json"); 

before repeating JSON in a script, and jQuery will automatically parse the response.

+2
source

You can also set request headers in a jQuery ajax call to the beforeSend function, as follows

 beforeSend: function (xhr) { xhr.setRequestHeader('Content-Type', 'application/json;charset=utf-8'); xhr.setRequestHeader('Accept', 'application/json'); } 

So you strictly declare json data type

0
source

Source: https://habr.com/ru/post/1238905/


All Articles