0

I am sending some variables from mypage.php to ajax.php page. On ajax.php I check some conditions and compiled some HTML based on request. Now I need to return that formatted HTML back to mypage.php and display. I do see request sent and response in my dev tools, however nothing seems to render on return. What am I missing?

mypage.php

<div id="myContainer"></div>


// JS
$.ajax({
    type: "POST",
    url : "ajax.php",
    data: {
        "op": "html",
        "wd": wd
    },
    dataType : "json",
    success: function(response)
        $("#myContainer").append(response.images);
    }
}); 

ajax.php

// php code here creating $images

echo '{ "images": "'.$images.'" }';
santa
  • 12,234
  • 49
  • 155
  • 255
  • 1
    What do you see if you console.log `response` in the success callback? – j08691 Jun 03 '19 at 19:26
  • I've added test console.log("test"); and I see it come up in console, so i know it's success. I don't get any errors or warnings. – santa Jun 03 '19 at 19:34
  • Don't console.log `test` which has no meaning, log the `response` variable that should contain the response from your script. E.g. `console.log(response, typeof response)` – j08691 Jun 03 '19 at 19:36
  • I think I have something in HTML that breaks it. When I use base64_encode on php side I see the string output. I guess I need to encode with PHP and decode with JS to see the output. Is there such a thing? – santa Jun 03 '19 at 19:39
  • seems atob() works. – santa Jun 03 '19 at 19:41
  • Don't construct JSON by hand. Use `echo json_encode(["images" => $images]);` – Barmar Jun 03 '19 at 20:37
  • base64_encode ur image & return back # https://stackoverflow.com/questions/12144479/ajax-and-returning-image-created-by-php-gd – jupeter Jun 04 '19 at 07:07

0 Answers0