Problems, need help? Have a tip or advice? Post it here.
20 posts Page 1 of 2
Will an Ajax call to get data from a Json file work on localhost server. Thank you
Yes, it should work. :)
I keep getting this (textStatus) parse error: and when I viewed the source page I got this message written in red:
SyntaxError: JSON.parse: unexpected non-whitespace character after JSON data at line 9 column 1 of the JSON data. I have crosschecked a billion times but dont find any character but Ajax still wont work
I am sure you must start with debugging the incoming response. Perhaps, it consists of something unexpected.
@adimpressions, adding to what @trendoman suggested - please use a JSON valdator (e.g.
https://jsonformatter.curiousconcept.com/) to find out exactly what is wrong with your JSON output.

Hope it helps.
I have done the json formatter bit and the json-data seems fine. I think I have found what could be causing my Ajax calls inability to parse the json data:
When I paste this into my json-data.php and make the Ajax call, everything works perfectly"
Code: Select all
{
"pages": [           
{
"page_title": "Daniel Goodman",
"page_date":  "June 12, 2017",
"page_link":  "http://localhost/glota/nominee.php?p=8",
"page_folder_name":  "overall-logistics-transport-excellence-award"
}
,{
"page_title": "Francis Abban",
"page_date":  "June 11, 2017",
"page_link":  "http://localhost/glota/nominee.php?p=4",
"page_folder_name":  "overall-logistics-transport-excellence-award"
}
]
}


But as soon as I make the jason-data.php file a couch managed template by pasting this and registering the template, it throws ths error: parsererror
Code: Select all
<?php require_once("glota/cms.php"); ?>
<cms:content_type 'application/json'/>
<cms:template hidden='1' title='JSON Data'/>

<cms:set my_template='nominee.php' />

<cms:set my_folder="<cms:gpc 'folder' />" />
<cms:if "<cms:folder_exists my_folder masterpage=my_template />" >
{
"pages": [           
<cms:pages masterpage=my_template folder=my_folder>
{"page_title": "<cms:addslashes><cms:show k_page_title/></cms:addslashes>",
"page_date":  "<cms:date k_page_date 'F j, Y'/>",
"page_link":  "<cms:show k_page_link/>",
"page_folder_name":  "<cms:show k_page_foldername/>"
}
<cms:if "<cms:not k_paginated_bottom/>">,</cms:if>
</cms:pages>
]
}
<cms:else />
    <cms:abort is_404='1' />
</cms:if>
<?php COUCH::invoke(); ?>


Still don't know why that happens... :?:
Hi, could you post a returned output of ajax call? (I.e. console.log(data) in your js script)
Hello trendoman, console.log(data) outputs a blank page . Nothing at all
@adimpressions, could you please post the exact AJAX code you are using (and the JSON generation code as well if it has changed from what you posted earlier).

Since your installation is not yet online, we'll try to replicate the problem on our systems.
Hello KK,
This is the Json generation code:
Code: Select all
<?php require_once("glota/cms.php"); ?>
<cms:content_type 'application/json'/>
<cms:template hidden='1' title='JSON Data'/>

<cms:set my_template='nominee.php' />

<cms:set my_folder="<cms:gpc 'folder' />" />
<cms:if "<cms:folder_exists my_folder masterpage=my_template />" >
{
"pages": [           
<cms:pages masterpage=my_template folder=my_folder>
{"page_title": "<cms:addslashes><cms:show k_page_title/></cms:addslashes>",
"page_date":  "<cms:date k_page_date 'F j, Y'/>",
"page_link":  "<cms:show k_page_link/>",
"page_folder_name":  "<cms:show k_page_foldername/>"
}
<cms:if "<cms:not k_paginated_bottom/>">,</cms:if>
</cms:pages>
]
}
<cms:else />
    <cms:abort is_404='1' />
</cms:if>
<?php COUCH::invoke(); ?>


And the Ajax code too:
Code: Select all
var nominee_list = $('#listpages');
   var categoryLink = $("#folders li a");
   
   categoryLink.each(function(){
      $(this).on('click', function(e){
         e.preventDefault();
         var jsonurl = $(this).attr('href');
         
         $.ajax({
            url: jsonurl,
            dataType: 'json',
            type: 'GET'}).
            done(function(data){
            console.log(data);
            alert('connection successful');
               var items = "";
               
               for (var i = 0; i < data.pages.length; i++) {
               items += '<li>';
                    items += '<a href="' + data.pages[i].page_link + '">' + data.pages[i].page_title + '</a>';
                    items += ' published on <em>' + data.pages[i].page_date + '</em>';
                    items += '</li>';
               }
               
               nominee_list.html(items);
            
         }).fail(function (jqXHR, textStatus){
            nominee_list.html("Response " + textStatus);
            });
      });
   });


Thanks
20 posts Page 1 of 2
cron