Error: [$parse:syntax] from JSON echo'd from PHP. Angularjs min 1.2.12

170 views
Skip to first unread message

Trash Match

unread,
May 15, 2014, 9:36:17 PM5/15/14
to ang...@googlegroups.com
I'm getting an angularjs error in my firebug console from JSON echo'd out with PHP.  Even when I strip out ALL references to angularjs, that PHP file STILL gives me the same error.  I'm guessing that angularjs is somehow monitoring anything that looks like JSON data, and trying to parse it?  I don't know.  This is the error:

Error: [$parse:syntax] http://errors.angularjs.org/1.2.12/$parse/syntax? etc., etc.

If I comment out the PHP echo statement, the error goes away.  The error stops execution of my code, and it took me a long time to isolate the line of code that was causing the error.  The other really strange thing, is that if I ECHO out some BAD JSON, I don't get an error.  If I echo some corrected JSON, I get an error.

If this is echo'd out with PHP, I get NO error.  This has no matching bracket on the end.

All store Listings: {{"0" : {"ad":"Rocker","ac":51,"af":"4","ah":"IP Holder","ab":"UnKnown","ae":"Wooden Rocking Chair","ag":"Looks Good","aa":"D14169LkGmw"},{"1" : {"ad":"Car","ac":26900,"al":"jpg","af":"5","ai":"jpg","ah":"IP Holder","ab":"Toyota","ak":"jpg","meet":"Atlanta","ae":"2013 Toyota Camry Hybrid Sedan","ag":"Mint Looks","aa":"E320141119PW93","aj":"jpg"},{"2" : {"ad":"Rocker","ac":50,"af":"5","ah":"IP Holder","ab":"Unknown","ae":"Wooden rocking chair.","ag":"Looks Good","aa":"D14169LkGmw"},{"3" : {"ad":"Car","ac":23000,"al":"jpg","af":"5","ai":"jpg","ah":"IP Holder","ab":"Subaru","ak":"jpg","meet":"Detroit","ae":"2013 Subaru Impreza Hatchback","ag":"Mint Looks","aa":"E3201412UeR90","aj":"jpg"}

If this is echo'd out with PHP, I get the error, and my code gets killed.

All store Listings: {{"0" : {"ad":"Rocker","ac":51,"af":"4","ah":"IP Holder","ab":"UnKnown","ae":"Wooden Rocking Chair","ag":"Looks Good","aa":"D14169LkGmw"},{"1" : {"ad":"Car","ac":26900,"al":"jpg","af":"5","ai":"jpg","ah":"IP Holder","ab":"Toyota","ak":"jpg","meet":"Atlanta","ae":"2013 Toyota Camry Hybrid Sedan","ag":"Mint Looks","aa":"E320141119PW93","aj":"jpg"},{"2" : {"ad":"Rocker","ac":50,"af":"5","ah":"IP Holder","ab":"Unknown","ae":"Wooden rocking chair.","ag":"Looks Good","aa":"D14169LkGmw"},{"3" : {"ad":"Car","ac":23000,"al":"jpg","af":"5","ai":"jpg","ah":"IP Holder","ab":"Subaru","ak":"jpg","meet":"Detroit","ae":"2013 Subaru Impreza Hatchback","ag":"Mint Looks","aa":"E3201412UeR90","aj":"jpg"}}

I'm concatenating arrays together as a string to create a multidimensional array.

        if (strlen($oneStoreListing) > 1) {
          //Keep adding single store listings to the array until the loop is done.
          $storeListingsData = $storeListingsData . trim($oneStoreListing) . ",";
          echo "<br>";
          var_dump($storeListingsData);
          echo "<br>";
        }


Then when the loop is finished, I remove the unneeded comma at the end and replace it with a second curly brace.

      $storeListingsData = substr($storeListingsData,0,-1);
      $storeListingsData = $storeListingsData . "}";


Is angularjs searching and parsing anything that looks like JSON as soon as the page is loaded?  That's what it seems like.  I'll try creating the JSON some other way, other than by concatenating text, and see if that makes a difference.  But, in any case, finding the cause of this error was a real pain.  It seems like a bug to me.

I just updated to angular.min.js 1.2.16 and I'm getting the same error.

Error: [$parse:syntax] http://errors.angularjs.org/1.2.16/$parse/syntax?

Trash Match

unread,
May 16, 2014, 12:17:21 AM5/16/14
to ang...@googlegroups.com
I just found out that my JSON was not right after all.  I had an extra curly brace at the beginning of every sub array.  That is part of my problem.

Trash Match

unread,
May 16, 2014, 12:32:29 AM5/16/14
to ang...@googlegroups.com
I figured out my problem.  I was constructing the JSON wrong.  I am going to construct the multi-dimensional array with text formulas after all.  I didn't have any luck pushing sub arrays into the array in PHP, then converting it back to JSON with JavaScript in the front end.  I'm going to add a counter as the array key, then a colon, then a comma at the end in a for loop:

$oneStoreListing = "\"" . $forCntrStore . "\":" . trim($oneStoreListing) . ",";

          //Keep adding single store listings to the array until the loop is done.
          $storeListingsData = $storeListingsData . $oneStoreListing;

Then when the entire array is created, I need to strip the comma off the end, and add a curly brace:

      $storeListingsData = substr($storeListingsData,0,-1);
      $storeListingsData = "{" . $storeListingsData . "}";

I'm not getting an error from angularjs, and javascript JSON is converting the array in string format to an object for angularjs:

data = JSON.parse(data);

Creates an object for angularjs.
Reply all
Reply to author
Forward
0 new messages