These three methods is a method in the JavaScript language, often used in front-end development.
A, qs.stringify () and qs.parse ()
qs npm is a warehouse management package can npm install qs
be installed command.
1. qs.parse () resolves the URL in the form of object
2. qs.stringify () into a target sequence form of a URL, to & splicing.
This method is particularly useful in some special cases. For example, when we use the hair axios post request to the server needs to pass parameters api, these parameters are often organized as an object, as shown in the following code:
axios({ method: 'post', url: 'comment', data:{ srcId : this.id, content: this.msg, add_time: new Date() } }). then(response => { //...... });
This code will error when executed, study its error, the server is unable to obtain the correct parameters to post them there.
You need to do is take parameters passed to the target server, using qs.stringify () for conversion. The correct code is as follows:
Axios ({ Method: 'POST' , URL: 'Comment' , qs.stringify ({: Data // parameter conversion to be passed SrcID: the this .id, Content: the this a .msg, ADD_TIME: new new a Date () }) } .) the then (Response => { });
二、JSON.stringify()和JSON.parse()
1.JSON.parse () The JavaScript Object Notation (JSON) string into a JSON object.
2.JSON.stringify () converts the JSON object represented as JavaScript Object Method (JSON) string
For example: when we go localstorage in persistent data, can only store strings, so if the data is to be stored js object, then you need to use JSON.stringify () into a string, and then localized storage
var cart = [{id: "102", price: 5528}, {id: 101,price: 268}]; localStorage.setItem('cart', JSON.stringify(cart));