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
  • 相关阅读:
    python函数
    python数据类型补充,copy知识点及文件的操作
    python数据类型
    python介绍
    Linux基础2-2 基础文件管理命令
    Linux基础2-1 根文件系统分析
    Linux基础1-3 命令使用帮助的获取
    Linux基础1-2 ls、cd、date、clock、cal、echo、printf命令使用简介
    Linux基础1-1
    fastDFS环境搭建
  • 原文地址:https://www.cnblogs.com/sumg/p/5649964.html
Copyright © 2011-2022 走看看