Prompt a simple working example of communication of these entities (json (file) ajax php).

<html> <head> <script type="text/javascript" src=" jquery/1.4.3/jquery.min.js"></script> </head> <body> <script type="text/javascript"> $(document).keyup(function(){ $.ajax({ url:'json.php', type:'POST', data:{'data':JSON.stringify(["Яблоко", "Апельсин", "Слива"])} }); </script> <form action=""><input type="search" id="search-text"> <input type="submit"></form> </body> </html> 



Where is the mistake? How to add a file here?

  • 2
    What file should I add here? What mistake? What swears: js or php? In order to get an answer to a question, you need to provide all the data on this issue .. Plus: first you need to google - such examples are a car and a small truck - Photon

2 answers 2

 <?php if(isset($_POST) && !empty($_POST['data'])) { var_dump(json_decode($_POST['data'])); die(); } ?> <html> <head> <script type="text/javascript" src=""></script> </head> <body> <script type="text/javascript"> $(document).ready(function(){ $.ajax({ url:'index.php', type:'POST', data:{'data':JSON.stringify(["Яблоко", "Апельсин", "Слива"])} }); }); </script> <form action=""><input type="search" id="search-text"> <input type="submit"></form> </body> </html> 
  • no, it does not work - ferz
  • You can also configure a web server, connect a working machine to the DC? - lampa
  • no, it all works. Now I will edit the question with your premier - ferz
  • without var_dump () just just echo json_decode ($ _ POST ['data']) - Artem
  • one
    @Photon on the fingers: as I understand it, a person has a file with text in json format. Instead of a file, I will convert the array to json format using JSON.stringify. In general, nothing was said about the file at the beginning. So think that the vehicle had in mind. - lampa
 $.ajax({ type: "POST", url: "/ajax/order", data: {id:'123123'} success: function(html){ var res = JSON.parse(html); alert(html); // просто вывод ответа alert(res); // распарсим JSON } }); 


 $options=array( "status"=>1, "err"=>$_POST['id'], ); echo json_encode($options); 
  • Send the Ajax data to the function as follows: data: "id = 123123", then $ _POST ['id'] will get 123123 but by sending something from pXp to the back of the javascript you can use json_encode (sometings), then the html variable from success: function ( html) {teach the value already in the form of an object (if necessary) - deivan_
  • Do I not understand something or is it plagiarism to my answer? - Artem
  • 2
    By the way, in order not to do JSON.parse (), you can simply do this: $ .ajax ({dataType: 'json', ....}); And then the ready array will immediately come to the success function - Photon
  • Can. Nobody forbids :) - Artem
  • Why plagiarism? Explanation. - ferz