Query-string encoding of a Javascript Object

前端 未结 30 3646
渐次进展
渐次进展 2020-11-22 00:23

Do you know a fast and simple way to encode a Javascript Object into a string that I can pass via a GET Request?

No jQuery, no

30条回答
  •  猫巷女王i
    2020-11-22 00:46

    You can also achieve this by using simple JavaScript.

    const stringData = '?name=Nikhil&surname=Mahirrao&age=30';
        
    const newData= {};
    stringData.replace('?', '').split('&').map((value) => {
      const temp = value.split('=');
      newData[temp[0]] = temp[1];
    });
    
    console.log('stringData: '+stringData);
    console.log('newData: ');
    console.log(newData);

提交回复
热议问题