Encountering a parse error when parsing JSON using getJSON

Looking to incorporate some jquery.gantt and it requires data in JSON format. The documentation can be found at ()

Here is the jQuery code snippet:

    $(".gantt").gantt({
        source: basePath + "system/print_gantt_project_data.php?project_id=" + projectID,
        navigate: "scroll",
        scale: "weeks",
        maxScale: "months",
        minScale: "days",
        itemsPerPage: 10,
        onItemClick: function(data) {
            alert("Item clicked - show some details");
        },
        onAddClick: function(dt, rowId) {
            alert("Empty space clicked - add an item!");
        },
        onRender: function() {
         console.log("chart rendered");
        }
    });

Below is the script that generates the JSON data:

$aryOutput = array();
if($project_tasks) {
    foreach($project_tasks as $aryTask) {
        $aryOutput[] =  array(
            'name' => $aryTask['pt_name'],
            'desc' => $aryTask['pt_name'],
            'values' => array(array(
            'to' => '/Date('.strtotime($aryTask['pt_end_date']).')/',
            'from' => '/Date('.strtotime($aryTask['pt_start_date']).')/',
            'desc' => $aryTask['pt_description'],
            'label' => $aryTask['pt_description']
        ))
        );
    }
}
$strJSON = json_encode($aryOutput);
header('Content-type:application/json;charset=utf-8');
echo $strJSON;

When inspecting the delivered JSON:

    [{"name":"test3","desc":"test3","values":[{"to":"\/Date(1442268000)\/","from":"\/Date(1442181600)\/","desc":"test3","label":"test3"}]},{"name":"test1","desc":"test1","values":[{"to":"\/Date(1442268000)\/","from":"\/Date(1442095200)\/","desc":"test1","label":"test1"}]},{"name":"test2","desc":"test2","values":[{"to":"\/Date(1442268000)\/","from":"\/Date(1442268000)\/","desc":"test2","label":"test2"}]}]

This output is from the console.log in Chrome. (according to the code inserted into jquery.fn.gantt.js:

            // **Create the chart**
            create: function (element) {

                // Initialize data with a json object or fetch via an xhr
                // request depending on `settings.source`
                if (typeof settings.source !== "string") {
                    element.data = settings.source;
                    core.init(element);
                } else {
                    $.getJSON(settings.source, function (jsData) {
                        element.data = jsData;
                        core.init(element);

                    })
  .fail(function(jqXHR, textStatus, errorThrown) { console.log('getJSON request failed! ' + textStatus); })
                }
            },

)

"getJSON request failed! parsererror"

The JSON structure appears valid, but the parse error indicates otherwise.

Any ideas?

Answer №1

Well, it turns out my initial assumption was incorrect. After investigating the console in Chrome using (shift+ctrl+i), I discovered the issue - an UTF8-BOM encoding problem. By converting my script file to ASCII/DOS format, everything started working perfectly.

Isn't it interesting how everyone praises UTF8 as the ultimate solution?

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Attempting to alter the background image through the action of clicking on an image using jQuery, HTML, and CSS

I have created custom weather icons like a sun, cloud, and rainy cloud using Photoshop. My goal is to allow users to click on these icons and change the background image of the website's body. However, despite my efforts, clicking on the images does n ...

How can I insert a zero value in front of single digit hours in Timepicker using Jquery?

My timepicker is set up like this: $('#datepickerTime').timepicker({ timeFormat : "hh:mm:ss" }); However, the issue arises when the time is between 1 and 9 because it does not automatically add a leading zero (e.g. 01:30). How can I make su ...

Is it possible to alter the color of an icon image using CSS?

I'm currently exploring ways to modify the color of an image that has a transparent and solid color split. My goal is to change the white portion for hover effects, and create a dynamic way to alter colors within WordPress without relying on Photosho ...

Understanding Arrays in Angular JSIn this article, we

I am new to working with Angular JS. Currently, I am populating an array and attempting to showcase its contents on the HTML page using ng-repeat. $scope.groupedMedia = []; // Elements are being added through a for loop. $scope.groupedMedia[year].push(r ...

The functionality to generate forms on the click of a button appears to be malfunctioning

I am currently attempting to dynamically create Bootstrap Panels using an `onclick` function. However, I want each Panel to generate multiple forms when the button is clicked. In my current code, the first Panel successfully creates forms when the button i ...

Creating a scrollable table with CSS: A step-by-step guide

My table has too many columns causing it to exceed the width of the screen. I would like to make it scrollable horizontally for a neater and more organized appearance. I believe CSS with the overflow hidden property can achieve this, but I am unsure where ...

Tips for choosing a subset of objects within a larger array of objects and generating a new array from them?

I am working with an array of 3586 objects, each containing various products. How can I create and store a new array that only includes the products from a specific index? console.log listarPorReferencia() { this.linxService.listarPorReferencia().subs ...

Display various post types in a WordPress loop

I am designing a website on Wordpress where I want to showcase various "tiles" containing content from different sections of the site on the homepage. These tiles are custom post types such as "services", "consultants", and "blog posts". While I understan ...

Convert your HTML document into a Microsoft Word file and see the total number of pages in

I have been experimenting with creating a Word document using HTML code. Thanks to various examples, I have made some progress. However, I am facing an issue where I cannot get the total number of pages to display as text on the first page of the document. ...

The Info Box in Google Maps seems to be ignoring the click event

What's the best method to add a click event to a Google Map? ...

Creating a counter while iterating over a JSON array in jq

I am currently working with jq-1.5. My goal is to generate a running counter (index) for a JSON array. The JSON data I have is: {"Actors": "Tom,Dick,Mary"} To split the string into an array, I am using splits(): echo '{"Actors": "Tom,Dick,Mary"}&a ...

extract non-alphanumeric characters from an array of strings using javascript

I am trying to loop over an array of strings in JavaScript and remove special characters from elements that contain them. I have been successful in achieving this with individual strings, but I am facing some challenges when working with arrays of string ...

Does the user need to download the scripts from Google API multiple times during the insertion process?

Concerned about the potential need for multiple downloads of a script from an unknown location on the user's computer, I would like to explore options to mitigate this issue. Specifically, I am considering creating a condition to check the download st ...

Arranging information according to the value of a specific field

I've encountered an issue while trying to sort my data. Despite using a simple query to order a table, the ordering doesn't seem to be functioning correctly. After successfully ordering the table, I aim to update the values in the first column us ...

Establishing a Protected Link to a Server through PHP

I'm working on accessing a database located on a server that I only have Remote Desktop access to. What steps do I need to take in order to query this database using my php web application? ...

How to extract a section of a string with AngularJS

Can someone help me figure out how to remove the last part of a string in order to obtain just the main word? For example, turning string(1) into string. Any suggestions would be appreciated! PS. Note that the string might look like this: sringggg(125). ...

Tips on maintaining the color of the favorite / unfavorite button even after refreshing the page

I am currently developing a Laravel project that allows users to favorite and unfavorite books. When a user clicks on the favorite button, the color changes to red. If clicked again, the color reverts back to gray. The database successfully updates without ...

Requirements needed to develop a mobile application for smartphones

I am currently developing a website using PHP and MySQL. My future plan includes creating a smartphone application that will allow users to connect with my website's database and receive notifications whenever new data is added to their accounts. What ...

PHP Hourly Loop Across Midnight

I've dedicated considerable time to finding a solution to creating a loop that will display the time between specific $start_time and $end_time values, increasing by one hour in the format HH:MM. For example, if the $start_time is "22:00" and $end_tim ...

Successful PHP submission confirmation

I have implemented a basic PHP script on my website to handle email sending. <?php $headers ="From:<$from>\n"; $headers.="MIME-Version: 1.0\n"; $headers.="Content-type: text/html; charset=iso 8859-1"; mail($to,$su ...