I have some JS that is sending a POST request to a PHP controller. The JS code is as follows:
我有一些JS正在向PHP控制器发送POST请求。 JS代码如下:
$.ajax({
url: 'map-controller/coordcontroller.php',
data: {myData:JSON.stringify(myArray)},
type: 'post',
success: function(output) {
console.log(output);
}
});
On the server side, $_POST["myData"] is as follows:
在服务器端,$ _POST [“myData”]如下:
[{"lat":36.8867497490586,"lng":-76.3046246767044},{"lat":36.88671756964517,"lng":-76.30381464958191}]
As I understand it, my data resides in a single element array ($_POST["myData"]), containing a series of objects delimited by commas. I have tried json_decode() but I have a feeling it isn't working because my data object is actually an array instead of actual JSON data.
据我了解,我的数据驻留在单个元素数组($ _POST [“myData”])中,包含一系列以逗号分隔的对象。我尝试过json_decode(),但我觉得它不起作用,因为我的数据对象实际上是一个数组而不是实际的JSON数据。
My question: How can I access each object within a loop? Or is my implementation flawed and should I modify my Javascript to send the data differently?
我的问题:如何访问循环中的每个对象?或者我的实现有缺陷,我应该修改我的Javascript以不同的方式发送数据吗?
1 个解决方案
#1
0
var_dump (json_decode($_POST["myData"]));
Results in:
结果是:
array(2) {
[0]=>
object(stdClass)#1 (2) {
["lat"]=>
float(36.886809817261)
["lng"]=>
float(-76.304672956467)
}
[1]=>
object(stdClass)#2 (2) {
["lat"]=>
float(36.886146919127)
["lng"]=>
float(-76.305075287819)
}
}
My mistake was using echo instead of var_dump to check the decoded object.
我的错误是使用echo而不是var_dump来检查解码的对象。
#1
0
var_dump (json_decode($_POST["myData"]));
Results in:
结果是:
array(2) {
[0]=>
object(stdClass)#1 (2) {
["lat"]=>
float(36.886809817261)
["lng"]=>
float(-76.304672956467)
}
[1]=>
object(stdClass)#2 (2) {
["lat"]=>
float(36.886146919127)
["lng"]=>
float(-76.305075287819)
}
}
My mistake was using echo instead of var_dump to check the decoded object.
我的错误是使用echo而不是var_dump来检查解码的对象。