AngularJs $http.post() 不发送数据 [英] AngularJs $http.post() does not send data

查看:30
本文介绍了AngularJs $http.post() 不发送数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

谁能告诉我为什么下面的语句没有将post数据发送到指定的url?当我打印 $_POST 时,在服务器上调用了 url - 我得到一个空数组.如果我在将消息添加到数据之前在控制台中打印消息 - 它会显示正确的内容.

Could anyone tell me why the following statement does not send the post data to the designated url? The url is called but on the server when I print $_POST - I get an empty array. If I print message in the console before adding it to the data - it shows the correct content.

$http.post('request-url',  { 'message' : message });

我也尝试过将数据作为字符串(结果相同):

I've also tried it with the data as string (with the same outcome):

$http.post('request-url',  "message=" + message);

当我以以下格式使用它时,它似乎有效:

It seem to be working when I use it in the following format:

$http({
    method: 'POST',
    url: 'request-url',
    data: "message=" + message,
    headers: {'Content-Type': 'application/x-www-form-urlencoded'}
});

但是有没有办法用 $http.post() 来做到这一点 - 我是否总是必须包含标题才能使其工作?我相信上面的内容类型是指定发送数据的格式,但我可以将它作为javascript对象发送吗?

but is there a way of doing it with the $http.post() - and do I always have to include the header in order for it to work? I believe that the above content type is specifying format of the sent data, but can I send it as javascript object?

推荐答案

我在使用 asp.net MVC 和 在这里找到解决方案

I had the same problem using asp.net MVC and found the solution here

AngularJS 的新手对于为什么$http 服务速记函数($http.post() 等)似乎没有可与 jQuery 等价物交换(jQuery.post() 等)

There is much confusion among newcomers to AngularJS as to why the $http service shorthand functions ($http.post(), etc.) don’t appear to be swappable with the jQuery equivalents (jQuery.post(), etc.)

区别在于jQueryAngularJS 如何序列化和传输数据.从根本上说,问题在于您选择的服务器语言无法在本机理解 AngularJS 的传输......默认情况下,jQuery 使用

The difference is in how jQuery and AngularJS serialize and transmit the data. Fundamentally, the problem lies with your server language of choice being unable to understand AngularJS’s transmission natively ... By default, jQuery transmits data using

Content-Type: x-www-form-urlencoded

以及熟悉的foo=bar&baz=moe序列化.

AngularJS,然而,使用

Content-Type: application/json 

and { "foo": "bar", "baz": "moe" }

JSON 序列化,不幸的是某些 Web 服务器语言——尤其是PHP——不要本地反序列化.

JSON serialization, which unfortunately some Web server languages—notably PHP—do not unserialize natively.

就像一个魅力.

代码

// Your app's root module...
angular.module('MyModule', [], function($httpProvider) {
  // Use x-www-form-urlencoded Content-Type
  $httpProvider.defaults.headers.post['Content-Type'] = 'application/x-www-form-urlencoded;charset=utf-8';

  /**
   * The workhorse; converts an object to x-www-form-urlencoded serialization.
   * @param {Object} obj
   * @return {String}
   */ 
  var param = function(obj) {
    var query = '', name, value, fullSubName, subName, subValue, innerObj, i;

    for(name in obj) {
      value = obj[name];

      if(value instanceof Array) {
        for(i=0; i<value.length; ++i) {
          subValue = value[i];
          fullSubName = name + '[' + i + ']';
          innerObj = {};
          innerObj[fullSubName] = subValue;
          query += param(innerObj) + '&';
        }
      }
      else if(value instanceof Object) {
        for(subName in value) {
          subValue = value[subName];
          fullSubName = name + '[' + subName + ']';
          innerObj = {};
          innerObj[fullSubName] = subValue;
          query += param(innerObj) + '&';
        }
      }
      else if(value !== undefined && value !== null)
        query += encodeURIComponent(name) + '=' + encodeURIComponent(value) + '&';
    }

    return query.length ? query.substr(0, query.length - 1) : query;
  };

  // Override $http service's default transformRequest
  $httpProvider.defaults.transformRequest = [function(data) {
    return angular.isObject(data) && String(data) !== '[object File]' ? param(data) : data;
  }];
});

这篇关于AngularJs $http.post() 不发送数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆