简体   繁体   中英

Sending JSON data to a server and back via AJAX - steps involved

I am trying to use JSON in my project web page so I can send data from user to server and back again on request. I am slightly confused on the order of operation for each JSON method. my current understanding is this:

On submitting data(1):

User inputs data and submits text → use JSON.stringify to put it in JSON → server receives data and JSON_decode($_POST) must be used to put it in readable PHP form.

On request to return the data(2):

User selects return data → PHP file uses JSON_encode(randomVariable); to turn it back into JSON form → JS file uses JSON.parse to turn it into readable JS form.

tldr; is what I have written above (1 & 2) the correct steps for sending and returning object data from user to server and vice versa.

Assuming you are talking about the use of AJAX requests, then yes the process and steps you've described are what you would need to do in order to send JSON data from browser to server, and back again.

PS You may find, depending on your application setup, that there are libraries / frameworks which will do the stringifying / decoding / encoding / parsing steps automatically as part of the data-exchange functions they expose - in reality of course they just wrap around the functions you've mentioned.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM