ionic/angular $http post form-data请求
发布时间:2020-12-17 10:11:11 所属栏目:安全 来源:网络整理
导读:主要有两个步骤,一是重置$http的heads参数,二是参数转换方法 $http的heads参数 var param = {url: '' ,method: 'post' } //transformRequestAsFormPost是自定义方法,代码在下面 param[ 'transformRequest' ] = transformRequestAsFormPost; //虽然transfo
主要有两个步骤,一是重置$http的heads参数,二是参数转换方法 $http的heads参数var param = {url:'',method:'post'}
//transformRequestAsFormPost是自定义方法,代码在下面
param['transformRequest'] = transformRequestAsFormPost;
//虽然transformRequestAsFormPost中也对headers做了改变,但不知道为什么不好用,自测在这里修改才好用
param['headers'] = { 'Content-Type': 'application/x-www-form-urlencoded; charset=UTF-8'};
transformRequestAsFormPostapp.factory(
"transformRequestAsFormPost",function() {
// I prepare the request data for the form post.
function transformRequest( data,getHeaders ) {
var headers = getHeaders();
headers[ "Content-type" ] = "application/x-www-form-urlencoded; charset=utf-8";
return( serializeData( data ) );
}
// Return the factory value.
return( transformRequest );
// ---
// PRVIATE METHODS.
// ---
// I serialize the given Object into a key-value pair string. This
// method expects an object and will default to the toString() method.
// --
// NOTE: This is an atered version of the jQuery.param() method which
// will serialize a data collection for Form posting.
// --
// https://github.com/jquery/jquery/blob/master/src/serialize.js#L45
function serializeData( data ) {
// If this is not an object,defer to native stringification.
if ( ! angular.isObject( data ) ) {
return( ( data == null ) ? "" : data.toString() );
}
var buffer = [];
// Serialize each key in the object.
for ( var name in data ) {
if ( ! data.hasOwnProperty( name ) ) {
continue;
}
var value = data[ name ];
buffer.push(
encodeURIComponent( name ) +
"=" +
encodeURIComponent( ( value == null ) ? "" : value )
);
}
// Serialize the buffer and clean it up for transportation.
var source = buffer
.join( "&" )
.replace( /%20/g,"+" )
;
return( source );
}
}
)
参考http://www.bennadel.com/blog/2615-posting-form-data-with-http-in-angularjs.htm
https://forum.ionicframework.com/t/ionic-framework-http-post-request/29195
(编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |