Returning a JSON object from PHP in an AJAX call?

Here my PHP code gets called during a jQuery AJAX call:

<?php include '../code_files/conn.php'; $conn = new Connection(); $query = 'SELECT Address_1, Address_2, City, State, OfficePhone1, OfficePhone2, Fax1, Fax2, Email_1, Email_2 FROM clients WHERE ID = ?'; $conn->mysqli->stmt_init(); $stmt = $conn->mysqli->prepare($query); $stmt->bind_param('s', $_POST['ID']); $stmt->execute(); $result = $stmt->get_result(); $row = $result->fetch_assoc(); echo json_encode($row); ?> 

And client code:

 $.post(url, {ID:$('#ddlClients').val()}, function(Result){ // Result } ); 

AJAX call completed successfully. I get the result value as

"{"Address_1":"Divisional Office 1","Address_2":"The XYZ Road",.....and so on

I want to be able to use the values ​​returned as Result.Address_1, Result.Address_2, etc. But I can not do this using the code above. I tried using $row = $result->fetch_object() and $row = $result->fetch_array() but didn't use it.

And I know that this can be done with this server side code:

 $row = $result->fetch_assoc(); $retVal = array("Address_1"=>$row['Address_1'], "Address_2"=>$row['Address_2'].......); echo json_encode($retVal); 

or

 $row = $result->fetch_object(); $retVal = array("Address_1"=>$row->Address_1, "Address_2"=>$row->Address_2.......); echo json_encode($retVal); 

Is there a way to send $row directly to the client side of JavaScript and is ready to use it as a JSON object without manually creating the array first?

+6
source share
3 answers

The response received from your PHP script is presented in plain text. However, you can $.parseJSON this string in an object using $.parseJSON in your callback function:

 $.ajax({ url : url,//note that this is setting the `url` property to the value of the `url` variable data : {ID:$('#ddlClients').val()}, type : 'post', success : function(Result){ var myObj = $.parseJSON(Result); //you can now access data like this: //myObj.Address_1 } } ); 

You can let jQuery do this for you by setting the dataType property of your AJAX call to json :

 $.ajax({ url : url//note that this is setting the `url` property to the value of the `url` variable data : {ID:$('#ddlClients').val()}, dataType : 'json', type : 'post', success : function(Result){ //you can now access data like this: //Result.Address_1 } } ); 

In the above examples, it is expected that the response from the server will be in this format (from your question):

 "{"Address_1":"Divisional Office 1","Address_2":"The XYZ Road"} 
+16
source

In your call to $.post last argument could be the data type: json :

 $.post(url, {ID:$('#ddlClients').val()}, function(Result){ alert(Result.Address_1); },'json' ); 

Everything should work then, since it looks like you are doing everything right.

+4
source

json_encode accepts objects, so there is no need to do this auto-build array:

 $row = $result->fetch_object(); echo json_encode($row); 

It is so simple!

+2
source

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


All Articles