Symfony send array with objects to javascript

huangapple go评论56阅读模式
英文:

Symfony send array with objects to javascript

问题

以下是翻译好的部分:

如果我在Ajax请求后使用JSON响应返回包含对象的数组,那么在Javascript中该数组为空。
数组的转储内容如下:

array:2 [
  0 => App\Entity\Firewalls {#744
    -id: 2
    -ip: "test.com"
    -user: "admin"
    -pass: "pw"
    -status: App\Entity\Status {#741
      -id: 2
      -status: "Finalize"
      -update: null
      -time: null
      -firewall: App\Entity\Firewalls {#744}
    }
  }
英文:

How is it possible to send an array of objects (entity) back to Javascript after an Ajax request?
If I return the array with the objects with a JSON response, the array is empty in Javascript.
Dump of array:

array:2 [
  0 => App\Entity\Firewalls {#744
    -id: 2
    -ip: "test.com"
    -user: "admin"
    -pass: "pw"
    -status: App\Entity\Status {#741
      -id: 2
      -status: "Finalize"
      -update: null
      -time: null
      -firewall: App\Entity\Firewalls {#744}
    }
  }

答案1

得分: 1

我会翻译以下代码部分:

$firewalls = ['your', 'firewalls'];
$json = json_encode($firewalls);
echo $json;

翻译为:

$firewalls = ['your', 'firewalls'];
$json = json_encode($firewalls);
echo $json;
$.ajax({
  type: "POST",
  url: "server.php",
  dataType: "json",
  success: function (firewalls) {
    console.log(firewalls);
  }
});

翻译为:

$.ajax({
  type: "POST",
  url: "server.php",
  dataType: "json",
  success: function (firewalls) {
    console.log(firewalls);
  }
});

如果数据类型设置为json,jQuery应该会自动解析它。如果不起作用,请随时分享浏览器的开发者工具显示的信息。

英文:

I'd encode the json first like

$firewalls = [ 'your', 'firewalls' ];
$json = json_encode($firewalls);
echo $json;

then parse it back

$.ajax({
  type: "POST",
  url: "server.php",
  dataType: "json",
  success: function (firewalls) {
    console.log(firewalls);
  }
});

if the datatype is set as json, jQuery should automatically parse it. If it doesn't work, feel free to share what the browser's developer tools say.

答案2

得分: 1

你是否已经安装了 composer require symfony/serializer?如果没有,我建议你使用它。

你可以使用Symfony对象规范化器将实体(或实体数组)序列化为JSON。

创建一个序列化器:

use Symfony\Component\Serializer\Encoder\JsonEncoder;
use Symfony\Component\Serializer\Encoder\XmlEncoder;
use Symfony\Component\Serializer\Normalizer\ObjectNormalizer;
use Symfony\Component\Serializer\Serializer;

$encoders = [new XmlEncoder(), new JsonEncoder()];
$normalizers = [new ObjectNormalizer()];

$serializer = new Serializer($normalizers, $encoders);

然后,将其用于你的实体或实体数组:

$this->serializer->serialize($data, 'json');

你可以在这里阅读更多关于这个的信息。

英文:

Do you have composer require symfony/serializer ? if not I recommend you use it.

You can the use symfony object normalizer and serialize an entity (or array of entities) into a json.

Create a serializer:

use Symfony\Component\Serializer\Encoder\JsonEncoder;
use Symfony\Component\Serializer\Encoder\XmlEncoder;
use Symfony\Component\Serializer\Normalizer\ObjectNormalizer;
use Symfony\Component\Serializer\Serializer;

$encoders = [new XmlEncoder(), new JsonEncoder()];
$normalizers = [new ObjectNormalizer()];

$serializer = new Serializer($normalizers, $encoders);

And use it with your entity or entities:

$this->serializer->serialize($data, 'json');

You can read more about this here.

huangapple
  • 本文由 发表于 2023年2月14日 06:44:07
  • 转载请务必保留本文链接:https://go.coder-hub.com/75441880.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定