0

I have a working Ajax utilized by JQuery and am changing what is listed on the page via a JSON response.

It works perfectly with text, however, now I plan to use the same code but have a video displayed instead where for example the mpg is displayed. However, it simply displays the text of the embed code from youtube instead.

Html page which gets JSON response:

 $(document).ready(function()
    {
            $("#submitbutton").click(function()
            {
                    $.ajax(
                    {
                            url: "https://example.com/jsonServer.php",
                            type: "GET",
                            dataType: "jsonp",
                            jsonp: "callback",
                            data: { carselection: $("#carselection").val()},
                            success: function(data)
                            {
                                    if(data.name == "error")
                                    {
                                            $("#outputcarname").text("There is no such car available"),
                                            $("#price").text(" "),
                                            $("#mpg").text(" ");
                                    }
                                    else
                                    {
                                            $("#outputcarname").text(data.name),
                                            $("#price").text(data.price),
                                            $("#mpg").text(data.mpg);
                                    }
                            },
                            error: function()
                            {
                                    $("#outputcarname").text("There is a problem with the server"),
                                    $("#price").text(" "),
                                    $("#mpg").text(" ");
                            }
                    });
            return false;


 });
});
</script>

JSON Server:

*note that I have replaced one of the mpg's with embed code straight from youtube, and again, this embed code displays exactly as is in the mpg area.

  <?php

    $cars = array('Maxima' => array('price' => '$32,780', 'mpg' => '24'),
                  'Prius' => array('price' => '$25,565', 'mpg' => '49'),
                  'Element' => array('price' => '$22,075', 'mpg' => '<iframe width="560" height="315" src="http://www.youtube.com/embed/enXTiYWQl-0" frameborder="0" allowfullscreen></iframe>'));

    if(array_key_exists($_GET['carselection'], $cars))
    {
            $carname = $_GET['carselection'];
            $results = array('name' => $carname, 'price' => $cars[$carname]['price'], 'mpg' => $cars[$carname]['mpg']);
    }
    else
    {
            $results = array('name' => 'error');
    }

    $callback = htmlentities($_GET['callback'], ENT_QUOTES);
    print $callback . '(' . json_encode($results) . ')';
    ?>

My hope was that the browser would accept the tags and display the video obviously, however I would guess the jquery.text is keeping it as truly text. Perhaps there is another function I am unaware of that I should be using?

ZAX
  • 968
  • 3
  • 21
  • 49

1 Answers1

2

Change this:

$("#mpg").text(data.mpg);

to:

$("#mpg").html(data.mpg);

.text() treats the string as a text.

.html() treats the string as html

jQuery: text() vs html()?

Community
  • 1
  • 1
Ibu
  • 42,752
  • 13
  • 76
  • 103