无法从服务器端的HttpServletRequest获取AngularJs fileData

时间:2016-02-24 10:34:19

标签: angularjs spring spring-mvc spring-restcontroller

AngularJS客户端:

userTab.factory('someService', ['apiService','$rootScope', function(apiService, $rootScope){
    return {
        updateData : function (someObj){
            return apiService.request({
                apiMethod: 'user/v1/updateData',
                httpMethod: 'POST',
                fileData: JSON.stringify(someObj)
            }).error(function(data, status) {
                throw "updateData error: " + status;
            });
        }
    }
}]);

服务器端代码:

Update.java

@Controller("user")
@RequestMapping(value = "/user/v1")
public interface Update
{
    @ResponseBody
    @ResponseStatus(HttpStatus.CREATED)
    @RequestMapping(value = "/users/{username}/updateData", method = POST)
    String updateData(HttpServletRequest request, HttpServletResponse response, @PathVariable("username") String username, @RequestBody UserUpdate userUpdate);
}

UpdateImpl.java

@Override
@Transactional
public String updateData(HttpServletRequest request, HttpServletResponse response, @PathVariable("username") String username,
        @RequestBody UserUpdate userUpdate) {

        String requestBody = ServletUtils.getRequestBody(request);
        logger.info("RequestBody: "+requestBody);

        return new String("true");
}

响应: RequestBody:{}

因为您看到RequestBody空白,因为我没有在angularjs API调用中给出params中的任何内容。

那么如何获得这里没有参考的fileData

更新

我的fileData是一个普通的json数组,就像这样:

{
  "todo" : "Dinner",
  "user" : [
    {
      "name" : "username",
      "password" : "passwordabc"
    }
  ],
  "notes : "some notes"
}

我在angularjs中创建了如下:

var object = {};
object.todo = "Dinner";
object.notes = "some notes";
userArray = [{
                name: 'myname',
                password: 'password'
            }];
object.user = userArray

更新

apiService.js

'use strict'; 

apiModule.factory('apiService', ['$http', '$q', 'LoginService','XSSValidator', function($http, $q, loginService, XSSValidator) {
    var basePath="https://somesite.com/userApi/";
    var _httpMethod="POST";
    var caching=false;
    var latestRequest={};
    $http.defaults.withCredentials = true;

    return{

    if(typeof bundle.fileData != "undefined"){
                return $http({
                    cache: caching,
                    method: bundle.httpMethod,
                    headers:{"Content-Type":contentType},
                    url:basePath+bundle.apiMethod, 
                    data:dataStr,
                    transformRequest: angular.identity,
                    transformResponse:bundle.transformResponse,
                    timeout: canceller.promise
                }).success(function(data, status, headers, config) {
                    //check to see if request has been redirected to login page, suggesting user has been logged out
                    if(typeof data==='string' && data.substr(0,44)==='<html><head><title>Login Page</title></head>'){
                        window.location.href="/login" + window.location.search;
                    }

                    var req;
                    if(config.data !== ""){
                        var requestArr=config.url.split(config.data);
                        req=requestArr[0].split("?")[0];
                    }
                    else{
                        req=config.url;
                    }
                    if(config.data !== "" && !data.fault){
                        if(Object.prototype.toString.call(data) == "[object String]"){
                            var msg = XSSValidator.validate("Response ", data);
                            if (msg != null) {
                                return $q.reject({ message: msg });
                            }
                        }else if(config.headers["Content-Type"] == "application/x-www-form-urlencoded" || config.headers["Content-Type"] == "application/json"){
                            eval(data);
                        }
                    }
                    if(latestRequest[req]!==config.url){
                        //cancel promise 
                        return $q.reject({ message: 'Rejecting this promise' });
                    }
                }).error(function(data, status, headers, config) {
                    if(status===401){ //user has been logged out server-side. redirect to login pg
                        window.location.href="/login" + window.location.search;
                    }
                    // called asynchronously if an error occurs
                    // or server returns response with an error status.
                });
            }

0 个答案:

没有答案