Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

AngularJs $http.post not posting in IE

Tags:

angularjs

This code:

        close: function (id) {
            var defered = $q.defer();

            $http.post('api/TopicSelection/Close', id).
                success(function (data) {
                    defered.resolve(data);
                }).
                error(function (data, status, headers, config) {
                    defered.reject(data.message);
                });

            return defered.promise;
        },

works perfectly in Chrome. However in IE 9, error callback is called straight away. data, status, headers are empty, and according to Fiddler/IE Network traffic, request is not sent.

What is wrong with it?

Tip

After some investigation it seems that IE 9 & AngularJS do not handle a case when I simply pass id as string in message body. When I changed it to:

$http.post('api/TopicSelection/Close', {id: id})

then the POST is sent with message body:

{"id":4611}

However, I don't want to have an object serialized. I want to have a simple request message body as string:

4611
like image 919
dragonfly Avatar asked Jun 26 '26 00:06

dragonfly


1 Answers

Issue resolved. id has to be string:

$http.post('api/TopicSelection/Close', id.toString())

Note from Angular's reference:

data – {string|Object} – Data to be sent as the request message data.

The only annoying thing is version without toString was not coherent betweet browsers...

like image 108
dragonfly Avatar answered Jun 29 '26 18:06

dragonfly