Ajax Response Json Print result in a table or in a div array

I have an ajax call in a php file that encodes an array into a json array / object. I am trying to print a json response in a table format or an array of DIVs. I am stuck on how to handle the response to ajax success. Here is my ajax ..

<script> $(document).ready(function(){ $("#adapter").keyup(function() { var adapter = $(this).val(); var dataString = 'searchword='+ adapter +'&format=json' ; if(adapter=='' || adapter < 2 ) { $("#display3").hide(''); } else { $.ajax({ type: "POST", url: "ajax/phpfile", data: dataString, cache: false, success: function(data) { var myObj = data; ///NOT how to print the result and decode in html or php/// } }); }return false; }); }); </script> 

Here is the json response from the server. I can warn the whole json response, so I know that it works on the ajax side ...

 {"Result":[{"ptos":{"PTOMasterID":"1","PTOMasterPart":"828B-U6805-L1CX","PTOSeriesUniqueID":"22","PTOPrice":"2715.78","PTOSeries":"82","PTOMounting":"8B","PTOTransmission":"U68","PTOSpeed":"05","PTOShifter":"L","PTOAssemblyID":"1","PTOShaftID":"C","PTOSpecialFeature":"X","PTODate":"2011-11-30 17:28:10"}},{"ptos":{"PTOMasterID":"2","PTOMasterPart":"828B-U6805-L3CX","PTOSeriesUniqueID":"22","PTOPrice":"2715.78","PTOSeries":"82","PTOMounting":"8B","PTOTransmission":"U68","PTOSpeed":"05","PTOShifter":"L","PTOAssemblyID":"3","PTOShaftID":"C","PTOSpecialFeature":"X","PTODate":"2011-11-30 17:28:10"}]} 
+4
source share
3 answers
 $(document).ready(function(){ $("#adapter").keyup(function() { var adapter = $(this).val(); var dataString = 'searchword='+ adapter +'&format=json' ; if(adapter=='' || adapter < 2 ) { $("#display3").hide(''); } else { $.ajax({ type: "POST", dataType: "json", //set this to json url: "ajax/phpfile", data: dataString, cache: false, success: function(data) { var myObj = data; ///NOT how to print the result and decode in html or php/// console.log(myObj); //to see the object } }); }return false; }); }); 

Alternatively, you can use JSON2.js, for example,

 JSON.parse(text, reviver) 

JSON 2 GITHUB

+1
source

You need to declare dataType: "JSON" if you are going to return JSON data from an ajax call. Then it's just var pto_id = data.ptos.PTOMasterID , and you can do $(object).html(pto_id); to enter a value. Hope that answers the questions you were looking for.

0
source

An interesting question - I found this online. It should be able to iterate over all objects and all properties of objects.

http://www.openjs.com/scripts/others/dump_function_php_print_r.php

0
source

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


All Articles