[英]Unable to PUT with Angularjs $http.put, receive Invalid JSON primitive
我有以下服務定義
app.factory('savedPropertiesService', ['$http', function ($http) {
var sessionId = $('input[name=SessionGuid]').val();
var contactId = $('input[name=ContactId]').val();
var savedPropertiesService = {};
savedPropertiesService.getSavedProperties = function () {
return $http.get("/Contact/SavedProperties?sessionGuid="+sessionId+"&contactId=" + contactId);
};
savedPropertiesService.refreshSavedProperties = function () {
return $http.get('/Contact/RefreshSavedProperties?sessionGuid=' + sessionId + '&contactId=' + contactId);
};
savedPropertiesService.deleteSavedProperty = function (listingKey) {
return $http['delete']('/Contact/DeleteSavedProperty?sessionGuid=' + sessionId + '&contactId=' + contactId + '&id=' + listingKey);
};
savedPropertiesService.updateSavedProperty = function (prop) {
return $http.put('/Contact/UpdateSavedProperty/', prop);
};
return savedPropertiesService;
}]);
像這樣在我的控制器中使用
$scope.$watch('items', function (newVal, oldVal) {
if (_.isEmpty(newVal) || _.isEmpty(oldVal)) return;
var prop = difference(newVal, oldVal);
savedPropertiesService.updateSavedProperty(prop)
.success(function (data) {
$scope.status = data;
})
.error(function (error) {
$scope.status = 'Unable to update saved properties data: ' + error.message;
});
}, true);
和服務端點(請不要判斷VB)
<HttpPut()>
Function UpdateSavedProperty(rating As RatingDto) As JsonResult
Return Json(ControlLibrary.CS.__PropDetails.ContactPropertiesDataFactory.UpdateSavedProperty(rating), JsonRequestBehavior.DenyGet)
End Function
無論我做什么,都永遠不會達到JSON.stringify或我的mvc3所指定的范圍,並且框架會引發異常。 System.ArgumentException:無效的JSON原語。
我什至只是嘗試發布一個手工制作的對象,以查看它是否會到達端點,但無濟於事。
有人對代碼可能有什么問題有任何建議嗎?
謝謝斯蒂芬
原來,我已經為$ http定義了一個全局轉換,它使用jQuery.param()進行編碼。 一旦我刪除了它,它就會完美地工作。
var app = angular.module('app', ['ui.select2', 'ui.bootstrap'])
.config(['$httpProvider', function ($httpProvider) {
delete $httpProvider.defaults.headers.common['X-Requested-With'];
$httpProvider.defaults.transformRequest = function (data) {
if (data === undefined) {
return data;
}
return $.param(data);
};
}]);
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.