Moondog
Moondog

Reputation: 11

JQuery AJAX call gives success, but response undefined

I have a rudimentary page from which I am calling a PHP service--using an AJAX call--to get an RSS feed and display it in a DIV tag.

I have a PHP page that returns the RSS feed as a JSON array. I have confirmed that the service works, as I can run it from the browser and get appropriate output.

But when I run it from the AJAX call, I get success, but undefined response. I have tried putting a callback function, but no joy. I'm sure the solution is pretty simple.

Here is the PHP page (TestService.php)

    <?php

header('Content-Type: application/json');


        $url = $_REQUEST['sUrl'];
        $year = $_REQUEST['getYear'];

        $rss = simplexml_load_file($url);
        $outlist = array();
           //echo json_encode($outlist);
           $i = 0;
        foreach ($rss->channel->item as $item)   {
           $i = $i + 1;
           $yr = date('Y', strtotime($item->pubDate));


            if ($yr == $year && $i < 5) {

            $outlist[] = array('title' => $item->title,
                            'pubDate' => $item->pubDate,
                            'description' => $item->description);
            }


       }
       //$serializedList = serialized($outlist);
       $encodedList = json_encode($outlist);
       echo $encodedList;



?>

Here is the HTML (TestPage.html):

<!DOCTYPE html>
<html>
   <head>
     <meta charset="utf-8">
      <title>JQuery RSS Demo</title>
      <script src="http://code.jquery.com/jquery-1.9.1.js"></script>
   </head>
   <body>
     <script>
        $(document).ready(function () {

        //set the initial URL call
        var sNewsUrl = "http://rss.cnn.com/rss/cnn_topstories.rss?xml";

        var today = new Date();
        var curYear = today.getFullYear();

        for (var yr = curYear; yr >= 2017; yr--) {
            $('#SelectYear').append("<option>" + yr + "</option>");
        }
        $('#news').html();
        $('#news').append("<li>" + sNewsUrl + "</li>");


            $.ajax({
                type: "POST",
                url: "TestService.php",
                contentType: "application/json; charset=utf-8",
                data: "{ 'sUrl': '" + sNewsUrl + "', 'getYear': '" + 
                          curYear + "' }",
                dataType: "json",
                success: function (response) {

                        var jsonData = JSON.parse(response.d);
                        $('#news').empty();

                        if (jsonData.length <= 0)
                            $('#divNews').hide();
                        else {
                            $('#divNews').show();
                            for (var i = 0; i < jsonData.length; i++) {
                                if (i < 3)
                                    $('#news').append("<li>" + 
                                       jsonData[i].pubDate + 
                                         " <a href='" + jsonData[i].link 
                                         + "' target='_blank'>" + 
                                         jsonData[i].title + "</a>" + "
                                         </li>");
                            }
                        }

                },
                error: function (xhr, status, error) {
                    //debugger;
                    alert("Result: " + status + " " + error + " " + 
                           xhr.status + " " + xhr.statusText)
                }
            });

});



<div id="divNews">
    News:<br />
    <ul id="news"></ul>

</div>

</body>
</html>

Upvotes: 1

Views: 29

Answers (0)

Related Questions