1

I'm using Angular's $http service to get some data from a remote data source, but logging out the results to the console gives empty strings.

The function looks as follows:

$http.get('/some/url/')
    .success(function(data/*, status, headers, config*/) {
        console.log('Success! data: ', data);
    })
    .error(function(data, status, headers, config) {
        console.log('failed http req!'); 
        console.log('data: ', data); 
        console.log('status: ', status); 
        console.log('headers: ', JSON.stringify(headers()));
        console.log('config: ', JSON.stringify(config));                         
    });

I'm purposfully calling a URL I know does not exist and expecting to get a 404. When running from the browser (using cordova serve) I see all the error data printed out to the console.log.

I'm using Cordova ~3.4 I've installed the console plugin for Cordova. I'm viewing the Android device log using adb logcat set to debugging.

Any ideas?

Update: I tried on all 4 variables to use JSON.stringify, just to see if that might work out of sheer luck in a moment of frustration... left them on the headers() and config since they're objects anyway. I still didn't get any print out on the data or status, which still puzzles me...

javinor
  • 674
  • 4
  • 8

3 Answers3

2

Cordova's console.log accepts only one argument (At least on Android) and that's why I've been getting only partial logging. Changing the code to:

$http.get('/some/url/')
    .success(function(data/*, status, headers, config*/) {
        console.log('Success! data: ' + data);
    })
    .error(function(data, status, headers, config) {
        console.log('failed http req!'); 
        console.log('data: ' + data); 
        console.log('status: ' + status); 
        console.log('headers: ' + JSON.stringify(headers()));
        console.log('config: ' + JSON.stringify(config));                         
    });

solved the problem.

javinor
  • 674
  • 4
  • 8
  • 1
    You are correct that on Android, console.log() uses only the first argument. This appears to be a limitation both of the Chromium webview and of the Android API: see how onConsoleMessage() can receive only one console message that has a single string: http://developer.android.com/reference/android/webkit/WebChromeClient.html#onConsoleMessage(android.webkit.ConsoleMessage) – cmarcelk Jun 02 '14 at 21:23
0

The success and error callbacks actually decompose the response object into 4 arguments and pass them as parameters.

You should use the .then syntax for handing success and error:

 $http(...).then(success,error)

The first argument to your error callback is the raw response object, which is in JSON format. You can extract some error information from it.

The success and error callbacks are convenient however, so moving this to a response interceptor might make more sense.

Michael Kang
  • 52,003
  • 16
  • 103
  • 135
  • Thanks for the input @pixelbits! I though the success and error methods of $http are identical (or at least equivalent) to using the then() method and are just more readable, no? – javinor Jun 02 '14 at 05:33
  • There are two main differences. First, then() returns a true promise object that you can chain with other promises, while success/error do not. Second, then() callbacks are passed a single response object (which is why you have to use response.data to get the return value), whereas the success/error callbacks are passed 4 args - which is a split up version of the response object. See this post for more details : http://stackoverflow.com/questions/16385278/angular-httppromise-difference-between-success-error-methods-and-thens-a – Michael Kang Jun 02 '14 at 06:44
0

Passing multiple args to console.log is not recommended way in Cordova. You can contact the log instead. The reason is, Cordova uses WebKit under the hook, and WebKit's onConsoleMessage method has only one argument and it will always print the first. You can find more details on official docs for android.

Kiran Maniya
  • 8,453
  • 9
  • 58
  • 81