Home > Blockchain >  Load Json String to Ajax using PHP
Load Json String to Ajax using PHP

Time:10-21

I have a string in PHP like below

$data = '{"post":{"fields":{"icon":{"height":768,"width":509,"url":"fetchdata16/images/d9/61/97/d96197470fc826c5a14e1f5c7497bcddd08ecf05f4c1b314360116650ab212e4.png","id":"fetchdata16/images/d9/61/97/d96197470fc826c5a14e1f5c7497bcddd08ecf05f4c1b314360116650ab212e4.png","format":"png"},"image":{"height":768,"width":509,"url":"fetchdata16/images/d9/61/97/d96197470fc826c5a14e1f5c7497bcddd08ecf05f4c1b314360116650ab212e4.png","id":"fetchdata16/images/d9/61/97/d96197470fc826c5a14e1f5c7497bcddd08ecf05f4c1b314360116650ab212e4.png","format":"png"},"title":"દુઃખી થવાનો એ રસ્તો","hashtags":[{"title":"ViralLatest","id":""},{"title":"ViralThought For the DayLatest","id":""}]},"locations":[""],"language":"gu","type":"IMAGE","tags":{"dhTags":{"genre":["G300"],"subGenre":["SG326"]}},"ttl":{"id":"2","name":"Infinite","type":"NORMAL","value":"31536000"},"action":"submit","postId":null,"updatePublishedDate":false},"userId":33555}';

I am trying to send it using ajax like below

<script>
var settings = {
  "url": "https://example.com/update",
  "method": "POST",
  "timeout": 0,
  "data": "<?php echo $data;?>",
  "dataType": "json",
};

$.ajax(settings).done(function (response) {
  console.log(response);
});
</script>

But its always giving me property missing error on line called data in console. if I use data like below

"data": "{\"post\":{\"fields\":{\"icon\":{\"height\":768,\"width\":509,\"url\":\"fetchdata16/images/d9/61/97/d96197470fc826c5a14e1f5c7497bcddd08ecf05f4c1b314360116650ab212e4.png\",\"id\":\"fetchdata16/images/d9/61/97/d96197470fc826c5a14e1f5c7497bcddd08ecf05f4c1b314360116650ab212e4.png\",\"format\":\"png\"},\"image\":{\"height\":768,\"width\":509,\"url\":\"fetchdata16/images/d9/61/97/d96197470fc826c5a14e1f5c7497bcddd08ecf05f4c1b314360116650ab212e4.png\",\"id\":\"fetchdata16/images/d9/61/97/d96197470fc826c5a14e1f5c7497bcddd08ecf05f4c1b314360116650ab212e4.png\",\"format\":\"png\"},\"title\":\"દુઃખી થવાનો એ રસ્તો\",\"hashtags\":[{\"title\":\"ViralLatest\",\"id\":\"\"},{\"title\":\"ViralThought For the DayLatest\",\"id\":\"\"}]},\"locations\":[\"\"],\"language\":\"gu\",\"type\":\"IMAGE\",\"tags\":{\"dhTags\":{\"genre\":[\"G300\"],\"subGenre\":[\"SG326\"]}},\"ttl\":{\"id\":\"2\",\"name\":\"Infinite\",\"type\":\"NORMAL\",\"value\":\"31536000\"},\"action\":\"submit\",\"postId\":null,\"updatePublishedDate\":false},\"userId\":33555}",

Its work fine but I do not know How I can convert my PHP string to above format. Let me know if anyone help me for solve the puzzle. Thanks!

CodePudding user response:

Not sure if you really need to have a string. You can simply create an object and assign it to your data variable. Looks like you already have the format that you need, so you only need to remove your quotes

However if you need to convert string to JSON in PHP you can use json_decode function

$json = '{"a":1,"b":2,"c":3,"d":4,"e":5}';

var_dump(json_decode($json));

If you want to convert your string to JSON with JS, you can use JSON.stringify

<script>
var settings = {
  "url": "https://example.com/update",
  "method": "POST",
  "timeout": 0,
  "data": JSON.stringify("<?php echo $data;?>"),
  "dataType": "json",
};

$.ajax(settings).done(function (response) {
  console.log(response);
});
</script>
  • Related