Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

I have a scenario in which I have to deal with a misconfigured server, which only understands a stringified JSON. What works with the server is:

    var req = JSON.stringify({id: 0, method: 'getToken', params: ['something', 'password', 'some_more_randomness']});
    $http({
        url: 'http://something.com/API',
        method: 'POST',
        data: req,
        responseType: 'json'
    }).success(function (data) {
        console.log('data', data);
    }); // works

And I've tried to use $resources to take the hassle out of the picture, and I have something like:

var apiService = angular.module('apiService', ['ngResource']);

/**
* apiService Module
*
*/

apiService.factory('API', ['$resource', function ($resource) {
    return $resource('http://somethng.com/API', {}, {
        getToken: {
            method: 'POST',
            params: JSON.stringify({id: 0, method: 'getToken', params: ['something', 'password', 'some_more_randomness']})
        }
    });
}]);

Which doesn't work. The server cannot parse the parameters. I'm afraid I don't have access to the server to allow posting properly to it.

What I'd like to do, is to make the exact request from within the service. Other variations – enclosing JSON.stringify inside [] doesn't help either.

What can be done here?

share|improve this question
1  
Did you try: JSON.stringify({data: {id: 0, method: 'getToken', params: ['something', 'password', 'some_more_randomness']}}) ? –  Maxim Shoustin Mar 13 at 15:54
    
@MaximShoustin params: JSON.stringify({data: {id: 0, method: 'getToken', params: ['something', 'password', 'some_more_randomness']}}) doesn't work –  Ashesh Mar 13 at 15:59
add comment

1 Answer 1

Fixed this, the best way to go here is the following;

instead of

API.getToken();

We need to pass in the data;

API.getToken({}, {id: 0, method: 'getToken', params: ['something', 'pass', 'some_more_randomness']});

This is not ideal for my situation, because the whole point of creating a new service was to write a wrapper over the API calls, but yeah, we might need one service to call another, which just does the basic calls.

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.