简体   繁体   English

无法使用Angularjs $ http.put放置,接收到无效的JSON原语

[英]Unable to PUT with Angularjs $http.put, receive Invalid JSON primitive

I have the following service define 我有以下服务定义

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;
}]);

and it is being used in my controller like so 像这样在我的控制器中使用

$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);

and the service endpoint (please don't judge the VB) 和服务端点(请不要判断VB)

<HttpPut()>
Function UpdateSavedProperty(rating As RatingDto) As JsonResult
    Return Json(ControlLibrary.CS.__PropDetails.ContactPropertiesDataFactory.UpdateSavedProperty(rating), JsonRequestBehavior.DenyGet)
End Function

No matter what I do, JSON.stringify or not my mvc3 enpoint is NEVER reached and the frameworks throws an exception. 无论我做什么,都永远不会达到JSON.stringify或我的mvc3所指定的范围,并且框架会引发异常。 System.ArgumentException: Invalid JSON primitive. System.ArgumentException:无效的JSON原语。

I've even just tried to post a hand crafted object to see if it will make it to the endpoint, all to no avail. 我什至只是尝试发布一个手工制作的对象,以查看它是否会到达端点,但无济于事。

Does anybody have any suggestions on what might be wrong with the code? 有人对代码可能有什么问题有任何建议吗?

Thank you, Stephen 谢谢斯蒂芬

Turns out I had defined a global transform for $http and it was using jQuery.param() to encode. 原来,我已经为$ http定义了一个全局转换,它使用jQuery.param()进行编码。 Once I removed this it work perfectly. 一旦我删除了它,它就会完美地工作。

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM