zoukankan      html  css  js  c++  java
  • $http post传值的问题

     1  var app = angular.module("myApp", [], function ($httpProvider) {
    2 $httpProvider.defaults.headers.post['Content-Type'] = 'application/x-www-form-urlencoded;charset=utf-8'; 3 /** 4 * The workhorse; converts an object to x-www-form-urlencoded serialization. 5 * @param {Object} obj 6 * @return {String} 7 */ 8 var param = function (obj) { 9 var query = '', name, value, fullSubName, subName, subValue, innerObj, i; 10 11 for (name in obj) { 12 value = obj[name]; 13 14 if (value instanceof Array) { 15 for (i = 0; i < value.length; ++i) { 16 subValue = value[i]; 17 fullSubName = name + '[' + i + ']'; 18 innerObj = {}; 19 innerObj[fullSubName] = subValue; 20 query += param(innerObj) + '&'; 21 } 22 } 23 else if (value instanceof Object) { 24 for (subName in value) { 25 subValue = value[subName]; 26 fullSubName = name + '[' + subName + ']'; 27 innerObj = {}; 28 innerObj[fullSubName] = subValue; 29 query += param(innerObj) + '&'; 30 } 31 } 32 else if (value !== undefined && value !== null) 33 query += encodeURIComponent(name) + '=' + encodeURIComponent(value) + '&'; 34 } 35 36 return query.length ? query.substr(0, query.length - 1) : query; 37 }; 38 39 // Override $http service's default transformRequest 40 $httpProvider.defaults.transformRequest = [function (data) { 41 return angular.isObject(data) && String(data) !== '[object File]' ? param(data) : data; 42 } ]; 43 44 });
     1   app.controller("addController", ["$scope", "$http", function ($scope, $http) {
     2 
     3             $scope.save = function () {
     4                 if ($scope.myForm.$valid) {
     5                     var data = { "name": $scope.txtname, "address": $scope.txtaddress, "phone": $scope.txtphone, "zipcode": $scope.txtzipcode, "isdefault": $scope.cbxisdefault };
     6                     $http.post("AddUserAddress", data).success(function (result) {
     7                         alert(result);
     8                     });
     9                 }
    10             }
    11         } ]);

    只有配置了$http的默认文档类型才能再后台用Request来获取提交的参数

    Content-Type
  • 相关阅读:
    安装jupyter
    git 查看分支图
    Docker原生健康检查使用
    压力测试指标判定
    Docker限制日志
    docker link 过时不再用了?那容器互联、服务发现怎么办?(2017年文章,建议使用docker network自定义网络)
    Nginx配置TCP服务负载均衡
    【转让】看看有你喜欢的书籍嘛?--都是我翻过的。
    《SOD框架企业级应用数据架构实战》新书简介和预定
    一年之计在于春,2015开篇:PDF.NET SOD Ver 5.1完全开源
  • 原文地址:https://www.cnblogs.com/sumg/p/5649964.html
Copyright © 2011-2022 走看看