Skip to content

Instantly share code, notes, and snippets.

@hayajo
Created December 16, 2013 05:27
Show Gist options
  • Save hayajo/7982694 to your computer and use it in GitHub Desktop.
Save hayajo/7982694 to your computer and use it in GitHub Desktop.
AngularJS の $http.post でリクエストパラメータが JSON じゃ困っちゃう感じなのでデフォルトを form-urlencoded にする
var module = angular.module('myApp');
module.config(function ($httpProvider) {
$httpProvider.defaults.transformRequest = function(data){
if (data === undefined) {
return data;
}
return $.param(data);
}
$httpProvider.defaults.headers.post = {
'Content-Type': 'application/x-www-form-urlencoded; charset=UTF-8'
}
});
$http.post('/api/users/login', {
username: username,
password: password
}).
success(function(data) {
console.log(data);
}).
error(function(data, status) {
console.error(data, status);
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment