Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
297 views
in Technique[技术] by (71.8m points)

javascript - How localstorage data append in formData

I append my localstorage data in formData

let products = [];    
for (i = 0; i < localStorage.length; i++)
     {
      x = localStorage.key(i);
      if (x !='debugbar-time-new' && x !='debugbar-time')
      {
        item = JSON.parse(localStorage.getItem(x))
        products.push({'_id' : item['_id'] , 'title':item['title'], 'quantity':item['quantity']});
      }
    }
    formData.append('products',JSON.stringify(products))

now my data in this form

products, [{"_id":5,"title":"bicycle","quantity":1},{"_id":2,"title":"i7","quantity":3},{"_id":4,"title":"i5","quantity":1},{"_id":1,"title":"i5","quantity":37},{"_id":3,"title":"i7","quantity":1}]

I get data in my controller

$req = $this->request->getVar('products');

I receive data in encoded form. How do I convert it in array and other forms?

question from:https://stackoverflow.com/questions/65846760/how-localstorage-data-append-in-formdata

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

just decode your encoded data with true parameter

json_decode($this->request->getVar('products') , true);

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...