title: JSON.parse working with hardcoded strings but not with var
body: <p>I am trying to read the length of array inside a <code>JSON</code> document, but whenever I keep the string hardcoded, it works but when I fetch the string from external source into a <code>var</code> and parse it it is not working. Can some one help me with this</p>
  
  <p>Below is the source code of the .js file</p>
  
  <pre><code>var app=angular.module('product',[]);
  app.controller('ProductCtrl',['$scope','$location','$http',function($scope,$location,$http) {
  var url=$location.absUrl();
  var baseurl='/products';
  var indx=url.indexOf(baseurl) + baseurl.length + 1;
  var name=url.substr(indx);
  var resp=$http.get("/getProductDetails/" + name);   
  //sample product
  //{ "ProductName" : "Chains" , "Index" : 16 , "Images" : [ "IMG_07.jpg" , "IMG_08.jpg" , "IMG_05.jpg" , "IMG_04.jpg" , "IMG_03.jpg" , "IMG_06.jpg" , "IMG_02.jpg" , "IMG_01.jpg"]}
  var product_str='';     
  resp.success(function(data) {product_str = data;}); 
  $scope.product=JSON.parse(product_str);
  $scope.minindex=1;
  $scope.maxindex=$scope.product.Images.length;   
  }]);
  </code></pre>
  
  <p>whenever i try the above code, i am not getting the value in $scope.maxindex, but if i use the below code then this value gets populated correctly</p>
  
  <pre><code>var product_str='{ "ProductName" : "Chains" , "Index" : 16 , "Images" : [ "IMG_07.jpg" , "IMG_08.jpg" , "IMG_05.jpg" , "IMG_04.jpg" , "IMG_03.jpg" , "IMG_06.jpg" , "IMG_02.jpg" , "IMG_01.jpg"]}';     
  //resp.success(function(data) {product_str = data;});
  </code></pre>
  
  <p>Just checked the console log in chrome. below were the errors</p>
  
  <p>If I put $scope.product=JSON.parse(product_str) in the callback function then below errors are thrown</p>
  
  <pre><code>TypeError: Cannot read property 'Images' of undefined
      at new &lt;anonymous&gt; (productDetail.js:23)
      at Object.e [as invoke] (angular.js:4182)
      at $get.z.instance (angular.js:8445)
      at angular.js:7697
      at s (angular.js:331)
      at v (angular.js:7696)
      at g (angular.js:7075)
      at angular.js:6954
      at angular.js:1451
      at l.$get.l.$eval (angular.js:14388)(anonymous function) @ angular.js:11598$get @ angular.js:8548$get.l.$apply @ angular.js:14489(anonymous function) @ angular.js:1449e @ angular.js:4182d @ angular.js:1447sc @ angular.js:1467Jd @ angular.js:1361(anonymous function) @ angular.js:26086a @ angular.js:2741c @ angular.js:3011
  angular.js:11598 SyntaxError: Unexpected token o
      at Object.parse (native)
      at http://localhost:8080/js/productDetail.js:12:25
      at http://ajax.googleapis.com/ajax/libs/angularjs/1.3.10/angular.min.js:81:199
      at http://ajax.googleapis.com/ajax/libs/angularjs/1.3.10/angular.min.js:112:343
      at l.$get.l.$eval (http://ajax.googleapis.com/ajax/libs/angularjs/1.3.10/angular.min.js:126:193)
      at l.$get.l.$digest (http://ajax.googleapis.com/ajax/libs/angularjs/1.3.10/angular.min.js:123:286)
      at l.$get.l.$apply (http://ajax.googleapis.com/ajax/libs/angularjs/1.3.10/angular.min.js:126:471)
      at l (http://ajax.googleapis.com/ajax/libs/angularjs/1.3.10/angular.min.js:81:489)
      at O (http://ajax.googleapis.com/ajax/libs/angularjs/1.3.10/angular.min.js:86:99)
      at XMLHttpRequest.w.onload (http://ajax.googleapis.com/ajax/libs/angularjs/1.3.10/angular.min.js:87:124)
  </code></pre>
  
  <p>If I put $scope.product=JSON.parse(product_str) outside the callback function then below error is thrown</p>
  
  <pre><code>SyntaxError: Unexpected end of input
      at Object.parse (native)
      at new &lt;anonymous&gt; (http://localhost:8080/js/productDetail.js:15:22)
      at Object.e [as invoke] (http://ajax.googleapis.com/ajax/libs/angularjs/1.3.10/angular.min.js:37:96)
      at $get.z.instance (http://ajax.googleapis.com/ajax/libs/angularjs/1.3.10/angular.min.js:76:261)
      at http://ajax.googleapis.com/ajax/libs/angularjs/1.3.10/angular.min.js:59:206
      at s (http://ajax.googleapis.com/ajax/libs/angularjs/1.3.10/angular.min.js:7:408)
      at v (http://ajax.googleapis.com/ajax/libs/angularjs/1.3.10/angular.min.js:59:190)
      at g (http://ajax.googleapis.com/ajax/libs/angularjs/1.3.10/angular.min.js:52:9)
      at http://ajax.googleapis.com/ajax/libs/angularjs/1.3.10/angular.min.js:51:118
      at http://ajax.googleapis.com/ajax/libs/angularjs/1.3.10/angular.min.js:17:492(anonymous function) @ angular.js:11598$get @ angular.js:8548$get.l.$apply @ angular.js:14489(anonymous function) @ angular.js:1449e @ angular.js:4182d @ angular.js:1447sc @ angular.js:1467Jd @ angular.js:1361(anonymous function) @ angular.js:26086a @ angular.js:2741c @ angular.js:3011
  </code></pre>