2015-12-09 40 views
1

我有一個應用程序CordovaAngularJS

使用Angular向我的後端應用程序發送一個值(Spring REST)。我使用的方法是$ http.post


問題

當我嘗試將數據發送到我的服務器春天不會在我的實體設置的值。由於這個原因,我無法保存我的新數據。


角碼

我AngularJS代碼如下:

httpService.createInBackend = function (url, data, callback, errorCallback) { 
    http.post(url, data) 
      .then(function (success) { 
       callback(success); 
      }, function (error) { 
       errorCallback(error.data); 
      }); 
}; 

我用下面的參數:

網址:http://<location>:<port>/<application>/<web_socket_url>

數據:

data: { 
     { 
     "incidentTypeId": 5, 
     "priorityId": 1, 
     "creationDate": 1449676871234, 
     "userId": 1, 
     "latitude": <some valid location>, 
     "longitude": <some valid location> 
     } 
    }, 
    timeout: 4000 
} 

我用「數據」而不是「PARAMS」,因爲我想通過身體發送數據。我得到這個與PUT函數一起工作,這與這個函數沒什麼不同。


我的REST控制器

@RequestMapping(value = "/employee/new", method = RequestMethod.POST) 
    public NewEmployee createIncident(@RequestBody NewEmployee employee) { 

    return employee; 
} 

我NewEmployee模式

private int employeeId; 

private int priorityId; 

private String information; 

private Date creationDate; 

private int userId; 

private float longitude; 

private float latitude; 

角結果

當使用控制檯,我得到這個方法如下結果:

我送:

{ 
    "employeeId":5, 
    "priorityId":1, 
    "creationDate":1449677250732, 
    "userId":1, 
    "information": "hello world!", 
    "latitude":<some valid location>, 
    "longitude":<some valid location> 
} 

我收到:

{ 
    employeeId: 0 
    priorityId: 0 
    creationDate: null 
    userId: 0 
    information: null 
    latitude: 0 
    longitude: 0 
} 

郵遞員

我想同樣的事情郵差(谷歌chrome插件)和這樣我的代碼工作。由於這一點,我認爲這是我的AngularJS代碼的問題。


我已經試過

我曾嘗試使用以下AngularJS調用嘗試:

$http({ 
    method: 'POST', 
    url: url, 
    data: data, 
    timeout: 4000 
}).then(function (success) { 
    callback(success); 
}, function (error) { 
    errorCallback(error); 
}); 

然而,這並沒有改變結果。仍然只有空值。


有沒有人知道我在做什麼錯?

+0

什麼類型的內容類型是Spring REST期待的請求? –

+0

默認它必須是json – theodor

+0

我在我的Spring中(通過Maven)有Jackson,而且我使用@enableWebMvc,所以我可以自動反序列化Json。 –

回答

2

更新代碼

改變$ HTTP後,你的代碼將工作。崗位:

httpService.createInBackend = function (url, data, callback, errorCallback) { 
    http.post(url, data) 
      .then(function (success) { 
       callback(success); 
      }, function (error) { 
       errorCallback(error.data); 
      }); 
}; 

標準$ HTTP類型的角度:

$http({ 
    method: 'POST', 
    url: url, 
    data: data 
}).then(function (success) { 
    callback(success); 
}, function (error) { 
    errorCallback(error); 
}); 

在體內我的數據的發送仍然沒有奏效。


解決方案

與$ HTTP請求與上述標準方法的問題是,像它不接受的對象:

data: { 
     { 
     "incidentTypeId": 5, 
     "priorityId": 1, 
     "creationDate": 1449676871234, 
     "userId": 1, 
     "latitude": <some valid location>, 
     "longitude": <some valid location> 
     } 
    }, 
    timeout: 4000 
} 

我不得不這樣更改爲:

var incidentInformation = { 
    incidentTypeId: $scope.selectedIncident.id, 
    priorityId: $scope.priorityId, 
    information: $scope.information, 
    creationDate: Date.now(), 
    userId: JSON.parse(localStorage.getItem('user')).id, 
    latitude: location.latitude, 
    longitude: location.longitude  
}; 

並添加一個新行:

incidentInformation = JSON.stringify(incidentInformation); 

我可以直接設置該值作爲數據值: (超時也要做這種方式,並且不能在該數據對象中設置)

$http({ 
    method: 'POST', 
    url: url, 
    data: incidentInformation, 
    timeout: 4000 
}).then(function (success) { 
    callback(success); 
}, function (error) { 
    errorCallback(error); 
}); 

利用這種改變代碼後端現在可以正確接收數據,我的應用程序可以保存數據。

謝謝Yoogeeks建議的標準方法。奇怪的是,AngularJS $ http。「某種方法」的工作方式與標準方法不同。

4

您是否嘗試過不使用$ http的簡寫版本? 我認爲,如果你使用類似

$http({ 
    method: 'POST', 
    url: url, 
    data: JSON.stringify(data) 
}) 
.then(function (success) { 
    callback(success); 
}, function (error) { 
    errorCallback(error.data); 
}); 

其中

data = { 
    "employeeId":5, 
    "priorityId":1, 
    "creationDate":1449677250732, 
    "userId":1, 
    "information": "hello world!", 
    "latitude":<some valid location>, 
    "longitude":<some valid location> 
} 

Further reading...

+0

我剛試過這個,但不幸的是它沒有工作 –

+0

以下是數據的內容? '{「employeeId」:5,「priorityId」:1,「creationDate」:1449677250732,「userId」:1,「information」:「hello world!」,「緯度」:<某些有效位置> :<一些有效的位置>}' 您是否驗證了您在後端收到的內容? – yoogeeks