【问题标题】:AngularJs $http.post not posting in IEAngularJs $http.post 未在 IE 中发布
【发布时间】:2013-12-05 18:29:06
【问题描述】:

这段代码:

        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;
        },

在 Chrome 中完美运行。然而在 IE 9 中,错误回调被立即调用。 data、status、headers 为空,根据 Fiddler/IE 网络流量,不发送请求。

这有什么问题?

提示

经过一番调查,当我简单地将 id 作为字符串传递到消息正文中时,IE 9 和 AngularJS 似乎没有处理这种情况。当我将其更改为:

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

然后 POST 与消息正文一起发送:

{"id":4611}

但是,我不想序列化对象。我想要一个简单的请求消息正文作为字符串:

4611

【问题讨论】:

    标签: angularjs


    【解决方案1】:

    问题已解决。 id 必须是字符串

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

    来自 Angular 参考的注释:

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

    唯一烦人的是没有 toString 的版本在浏览器之间不连贯......

    【讨论】:

    • 我也有类似的问题,但 $http.get ... 仅在 IE
    【解决方案2】:

    试试这样的:

    var xsrf = $.param({"id": id});
    
            return $http({
              method  : 'POST',
              url     : 'api/TopicSelection/Close',
              data    : xsrf,
              headers : {'Content-Type' : 'application/x-www-form-urlencoded; charset=UTF-8'}
            });
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-10-15
      • 1970-01-01
      • 2016-12-02
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多