加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 综合聚焦 > 服务器 > 安全 > 正文

AngularJs $ http.post()不发送数据

发布时间:2020-12-17 09:35:49 所属栏目:安全 来源:网络整理
导读:有谁能告诉我为什么下面的语句不发送post数据到指定的url? url被调用,但在服务器上,当我打印$ _POST – 我得到一个空数组。如果我在将消息添加到数据之前在控制台中打印消息 – 它显示正确的内容。 $http.post('request-url',{ 'message' : message });
有谁能告诉我为什么下面的语句不发送post数据到指定的url? url被调用,但在服务器上,当我打印$ _POST – 我得到一个空数组。如果我在将消息添加到数据之前在控制台中打印消息 – 它显示正确的内容。
$http.post('request-url',{ 'message' : message });

我也试过它与数据作为字符串(具有相同的结果):

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

它似乎是工作,当我使用它在以下格式:

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

但是有一种方法使用$ http.post() – 并且我总是要包括标题才能工作?我相信上面的内容类型是指定发送数据的格式,但是我可以发送它作为javascript对象吗?

我有同样的问题使用asp.net MVC和 found the solution here

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.)

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

06000

and the familiar foo=bar&baz=moe serialization.

AngularJS,however,transmits data using

06001

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

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;
  }];
});

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读