Skip to content

Instantly share code, notes, and snippets.

@Mickey-
Created May 16, 2014 11:44
Show Gist options
  • Save Mickey-/25a7ecf3b2b1e7419c95 to your computer and use it in GitHub Desktop.
Save Mickey-/25a7ecf3b2b1e7419c95 to your computer and use it in GitHub Desktop.
让angular的$http服务像jQuery一样异步提交POST数据
呦呦 = ng.module('呦呦', [], ['$httpProvider', ($httpProvider) ->
#修改Content-Type
$httpProvider.defaults.headers.post['Content-Type'] = 'application/x-www-form-urlencoded;charset=utf-8'
serialize = (rawData) ->
tmpData = {}
queryString = ''
ng.forEach(rawData, (value, key) ->
if (ng.isArray(value) || ng.isArray(Object))
ng.forEach(value, (v, i) ->
dataNode = key + '[' + i + ']'
tmpData[dataNode] = v
queryString += serialize(tmpData) + '&'
)
else if (value != undefined && value != null)
queryString += encodeURIComponent(key) + '=' + encodeURIComponent(value) + '&'
)
return if queryString.length then queryString.substr(0, queryString.length - 1) else queryString
#修改序列化数据方法
$httpProvider.defaults.transformRequest = [(data) ->
return ng.isObject(data) && serialize(data)
]
])
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment