![](/img/trans.png)
[英]Getting error 415 (Unsupported Media Type) while posting data using $http.post() in AngularJs and Spring
[英]Getting error “415 Unsupported Media Type” while posting data in angularJS
我正在從事AngularJS的項目。 我在angularjs中使用$ http.post以“表單數據”類型發布數據。 但顯示錯誤:“ 415不支持的媒體類型”
angular.module('odeskApp')
.factory('Password', function ($http, $q) {
return {
update: function (pwd) {
var deferred = $q.defer();
$http.post('/api/user/password', { 'password': pwd }, {
headers: {
'Accept': '*/*',
'Content-Type': 'application/x-www-form-urlencoded; charset=UTF-8',
'X-Requested-With': 'XMLHttpRequest'
},
transformRequest: function(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 ); }
})
.success(function (data) {alert("success");
$('#changePasswordAlert .alert-success').show();
$('#changePasswordAlert .alert-danger').hide();
$('#changePasswordAlert .alert').mouseout(function(){ $(this).fadeOut('slow'); });
deferred.resolve(data); //resolve data
})
.error(function (err) {
$('#changePasswordAlert .alert-danger').show();
$('#changePasswordAlert .alert-success').hide();
deferred.reject();
$('#changePasswordAlert .alert').mouseout(function(){ $(this).fadeOut('slow'); });
});
return deferred.promise;
}
};
});
我不知道這里會發生什么。 我也定義了標題。 請提出任何可能的解決方案,或者如果需要任何其他信息,請告訴我。
問題可能是因為您將Content-Type
指定為'application/x-www-form-urlencoded; charset=UTF-8'
'application/x-www-form-urlencoded; charset=UTF-8'
但您要發送的數據是JSON對象。 因此,服務器接收的內容與預期的內容不同。 更改為:
Content-Type: 'application/json'
那應該修復錯誤。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.