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
233 views
in Technique[技术] by (71.8m points)

javascript - Array of objects turns into Array of strings after an AXIOS call to PHP

I'm using Axios to send a POST data to PHP. Within the call, I'm also sending a param which is a string that is converted into an array of objects using JOSN.parse():

params: {        
    'myParam': JSON.parse(string)
}

Everything works fine at this point. The string is properly converted into an array of objects.

When the param is being sent though, on the PHP side the array is still an array, but the objects have become strings.

Does someone have any clue about why?

question from:https://stackoverflow.com/questions/65545468/array-of-objects-turns-into-array-of-strings-after-an-axios-call-to-php

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

1 Reply

0 votes
by (71.8m points)

I figured that the right approach is the following:

Before the AXIOS call, apply JSON.stringify() to the array of objects.

'myParam': JSON.stringify(arrayOfObjects) // this will be the param sent from Axios

Then on the PHP side, when you intercept the POST, use json_decode() to turn the string into the original array of object.

json_decode($request->all()['myParam']

Clean and effective.


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

...