0

I am submitting an $http request to my server using this code below, yet all the of sudden Angular is complaining about an "Unexpected token , " somewhere.

function sendAggrRequestToServer(url) { var deferred = $q.defer(); $http({ method: 'GET', encoding: 'JSON', headers: { 'Access-Control-Allow-Origin': 'true' }, withCredentials: true, url: url }).success(function (data, status, headers, config) { var retval = data; deferred.resolve(retval); }).error(function (data, status, headers, config) { logErr("Error submitting aggregation request to server: " + status); }); return deferred.promise; } 

The strange thing is that this exact URL works successfully both in the browser and in Fiddler. And the Json data is returned as expected. However, for some strange reason my javascript code is throwing this exception:

 [app] [HT Error] Unexpected token , Object {exception: SyntaxError, cause: undefined} angular.js:9778 (anonymous function) angular.js:9778 logIt logger.js:55 logError logger.js:49 (anonymous function) logger.js:32 (anonymous function) config.exceptionHandler.js:26 deferred.promise.then.wrappedCallback angular.js:11322 (anonymous function) angular.js:11405 Scope.$eval angular.js:12412 Scope.$digest angular.js:12224 Scope.$apply angular.js:12516 done angular.js:8204 completeRequest angular.js:8412 xhr.onreadystatechange 

And my URL is :

 http://localhost:49479/api/aggr?sid=9630a8040ee6c901a4034c07894abc317272f855c757a4c60a6a&kri=[CDSStress%20A]:[USD%2010Y%20X%20-1.25],[CDSStress%20A]:[USD%2010Y%20X%201.25]&aggrFunc=SUM([CDSStress%20A]:[USD%2010Y%20X%20-1.25]),SUM([CDSStress%20A]:[USD%2010Y%20X%201.25])&dim=Counterparty 

FYI: All appears to be fine in angular.js at this point in angular, and I can indeed see the response data (which is definitely valid Json data). The callback status param has a value of "200" and the statusText is 'OK':

function completeRequest(callback, status, response, headersString, statusText) { // cancel timeout and subsequent timeout promise resolution timeoutId && $browserDefer.cancel(timeoutId); jsonpDone = xhr = null; // fix status code when it is 0 (0 status is undocumented). // Occurs when accessing file resources or on Android 4.1 stock browser // while retrieving files from application cache. if (status === 0) { status = response ? 200 : urlResolve(url).protocol == 'file' ? 404 : 0; } // normalize IE bug (http://bugs.jquery.com/ticket/1450) status = status === 1223 ? 204 : status; statusText = statusText || ''; callback(status, response, headersString, statusText); $browser.$$completeOutstandingRequest(noop); } 

};

Here is a screen image of the break point where I can inspect the "response" param:

enter image description here

However, as soon as it resolves the promise and returns to my calling function, angular throws the Unexpected token error.

I have been using this for days now, but today I must have introduced something to cause this. I just can't figure out what !

Your advice and guidance is greatly appreciated.

regards.

Bob

UPDATE:

I now find Angular throwing the actual exception inside $HttpProvider() on this line :

 data = fromJson(data); 

inside here :

function $HttpProvider() { var JSON_START = /^\s*(\[|\{[^\{])/, JSON_END = /[\}\]]\s*$/, PROTECTION_PREFIX = /^\)\]\}',?\n/, CONTENT_TYPE_APPLICATION_JSON = {'Content-Type': 'application/json;charset=utf-8'}; var defaults = this.defaults = { // transform incoming response data transformResponse: [function(data) { if (isString(data)) { // strip json vulnerability protection prefix data = data.replace(PROTECTION_PREFIX, ''); if (JSON_START.test(data) && JSON_END.test(data)) data = fromJson(data); } return data; }], // transform outgoing request data transformRequest: [function(d) { return isObject(d) && !isFile(d) && !isBlob(d) ? toJson(d) : d; }], // default headers headers: { common: { 'Accept': 'application/json, text/plain, */*' }, post: copy(CONTENT_TYPE_APPLICATION_JSON), put: copy(CONTENT_TYPE_APPLICATION_JSON), patch: copy(CONTENT_TYPE_APPLICATION_JSON) }, xsrfCookieName: 'XSRF-TOKEN', xsrfHeaderName: 'X-XSRF-TOKEN' }; 

And sendAggregationREquest() is called from here :

 function buildAndSendAggregationQuery() { // iterate list in reportsContext , pass data to to sendAggregationRequest() var userKRIs = vm.kriUserDataSource.get(); userKRIs.load(); var group; var kri = ''; var aggrFunc = ''; var aggrKriFuncArray; var dimension = vm.selectedDimen; var dimenMulti = ''; var applyScenarioAggr = false; if (vm.kriUserDataSource.data()[0].group == '99_HSVaR') { applyScenarioAggr = true; } // Call function to build the aggr function. Return value is an array. if (applyScenarioAggr) { aggrKriFuncArray = reportsContext.buildAggrFunc(vm.kriUserDataSource.data(), 'AVERAGE'); } else { aggrKriFuncArray = reportsContext.buildAggrFunc(vm.kriUserDataSource.data(), 'SUM'); } kri = aggrKriFuncArray[0]; aggrFunc = aggrKriFuncArray[1]; for (var i = 0; i < vm.multiSelectedDimen.length; i++) { dimenMulti += vm.multiSelectedDimen[i] + ','; } dimenMulti = dimenMulti.substr(0, dimenMulti.length - 1); // ' remove final "," sendAggregationRequest(kri, aggrFunc, dimension); //dimenMulti); } 

and finally, the response data that comes back from server prior to that exception:

 {"status":"SUCCESS", "messages":[], "data":[{"attributes":[{"name":"Counterparty","type":"string"},{"name":"SUM(CDSStress A:USD 10Y X -1.25)","type":"double"},{"name":"SUM(CDSStress A:USD 10Y X 1.25)","type":"double"}],"rows":[{"id":0,"values":["Goldman",0.,0.]},{"id":1,"values":["IBM",0.,0.]},{"id":2,"values":["JP Chase",0.,0.]},{"id":3,"values":["MINESCONDIDA",0.,0.]},{"id":4,"values":["Merrill",0.,0.]},{"id":5,"values":["Nokia",0.,0.]},{"id":6,"values":["Pequot",0.,0.]},{"id":7,"values":["Pimco Fund A",0.,0.]},{"id":8,"values":["Pimco Fund B",0.,0.]},{"id":9,"values":["Deutsche",0.,0.]},{"id":10,"values":["Ditech",0.,0.]},{"id":11,"values":["GM Isuzu",0.,0.]},{"id":12,"values":["GM Opel",0.,0.]},{"id":13,"values":["GMAC",0.,0.]},{"id":14,"values":["GMAC Insurance",0.,0.]},{"id":15,"values":["GSAM",0.,0.]},{"id":16,"values":["General Insurance",0.,0.]},{"id":17,"values":["Genworth",0.,0.]},{"id":18,"values":["AIG",0.,0.]},{"id":19,"values":["Andor",0.,0.]},{"id":20,"values":["BARCLAYS",92.7731197209214,-10.1717767200607]},{"id":21,"values":["BHHSH",0.,0.]},{"id":22,"values":["BHPBFIN",0.,0.]},{"id":23,"values":["BHPSTEEUR",1468.80370935,-161.395632863801]},{"id":24,"values":["BHPSUS",0.,0.]},{"id":25,"values":["BLUESCOPEFIN",0.,0.]},{"id":26,"values":["CSFB",3.35029024626419,-0.367366071961442]},{"id":27,"values":["BLOSOFL",0.,0.]},{"id":28,"values":["GRMOBND",0.,0.]}]}]} 
3
  • 1
    Can you post the calling function? Commented Sep 4, 2014 at 18:32
  • The function that calls sendAggregationRequest() you mean ? Commented Sep 4, 2014 at 18:39
  • Yes, show us where sendAggrRequestToServer has been called Commented Sep 4, 2014 at 18:46

1 Answer 1

1

can you make a jsfiddle ? or at least show us how you call the sendAggrRequestToServer method. It shouldbe

var promise = sendAggrRequestToServer(url); promise.then(function(data) { console.log('Success we got: ' + data); }, function(reason) { console.log('Failed we got: ' + reason); }, function(update) { console.log('Strange we got: ' + update); }); 

Thx

Sign up to request clarification or add additional context in comments.

4 Comments

I can't really make a jsfiddler because I have a server running which handles my request.
it's definitely the Json response which contains a problem. I just validated it at jsonlint.com
I agree for your JSon data.But do you know that you can build a jsfiddle example that echoes the data you send via $http.get if you use the particular url /echo/json/ ? With that you can do as if a remote server replies the data you sent in your call.
I need to look into how to use /echo/json. It sounds like a useful resource.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.