0

I am making a POST request:

var offers = $resource('/api/offers/:id', { 
    id: '@id' 
}

offers.save({}, { name: $scope.newOfferName }, function (offerId) {
    $location.path('/offers/' + offerId);
});

I expect offerId to be a string "key", but instead I get an array [0] "k", [1] "e", [2] "y".

On the backend I use Nancy and I return the response using:

Post["/"] = _ =>
{
    return Response.AsText("key");
};

The response Header say Content-Type:text/plain and in Chrome preview (Network tab) I can see "key".

When I return an object as JSON it's working fine, but I don't want to create fake class (having a string field) just to pass a string to the client.

I assume Nancy is fine here. What is happening with Angular?

2 Answers 2

2

You don't have to create a class for that. You can use an anonymous type:

Post["/"] = _ =>
{
    return Response.AsJson(new { offerId = "key" });
};
Sign up to request clarification or add additional context in comments.

1 Comment

Yes, that is what I will do, but the answer by @Zerot explains why is it like that, so I mark it as an answer.
1

angular has a default transform that tries to parse incoming data as json. https://github.com/angular/angular.js/blob/master/src/ng/http.js#L94

You could remove that transform from the transformers array in $http completely, or replace it with one that checks the content-type before trying to transform the data.

1 Comment

Good to know that '#L94' following Github link brings you to 94th line of code.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.