Reputation: 135
I have an object which converts my numbers into strings. I have done quite a bit of googling trying to figure why this occurs but am still unsure - I think it might have to do with JSON or header info in post method? So, my next attempt is to convert the object back to an integer. I saw couple options: parseInt(), Number(), split('""') but hasn't resolved my issue.
After Post: $rootScope.fullDay = { mon:"1", tue:"2"}
I need it to be in the following format: Before Post:
$rootScope.fullDay = { mon: 1, tue: 2 }
EDIT: The values are not in quotes as shown below:
<li class="col-md-3">
<input type="checkbox" id="Monday" ng-true-value= 1 ng-model="day.mon" ng-change="selectedDay()">
<label class="ui-checkbox" for="Monday">Monday</label>
</li>
<li class="col-md-3">
<input type="checkbox" id="Tuesday" ng-true-value= 2 ng-model="day.tue" ng-change="selectedDay()">
<label class="ui-checkbox" for="Tuesday">Tuesday</label>
JS file:
$scope.selectedDay = function () {
if (!$rootScope.edit) {
console.log($scope.day)
$rootScope.fullDay = $scope.day;
$rootScope.day = Object.keys($scope.day).map(key => $scope.day[key]);
$rootScope.day.forEach(function (item, index) {
if (item === false) {
$rootScope.day.splice(index, 1);
}
});
$rootScope.dayFormatted = $rootScope.day.join(' ');
$rootScope.schedule.weekdays.day['@'].day = $rootScope.dayFormatted;
}
POST (only including partial):
createJob_API: function () {
var data = $.param({"fullDay": $rootScope.fullDay, "fullDate": $rootScope.fullDate})
var settings = {
method: 'POST',
data: data,
headers: { 'Content-Type': 'application/x-www-form-urlencoded; charset=UTF-8' },
Here is an image of console log before POST: https://i.sstatic.net/2yl9d.jpg After: https://i.sstatic.net/MgB27.jpg Note: the second img is just an example of output not the same days selected as the before image.
Thanks for your help - I would appreciate if someone can tell me why the conversion happens automatically.
Upvotes: 1
Views: 1337
Reputation: 135
I was able to resolve my issue with the following:
Object.keys($rootScope.edit.fullDay).forEach(function(key,value){
$rootScope.edit.fullDay[key] = parseInt($rootScope.edit.fullDay[key]);
Upvotes: 0
Reputation: 943979
application/x-www-form-urlencoded
only supports key=value pairs. It doesn't distinguish between different data types. It supports only strings.
(A non-standard extension for it uses []
characters in the key name to represent arrays and objects, but there is nothing which distinguishes between strings and numbers).
If you want a number, then you'll have to convert the data to that format once it arrives on the server.
Upvotes: 2
Reputation: 1377
You're storing them as a string by putting them in the quotes, if you'd store them like this:
var obj = {
mon: 1,
tue: 2
};
You will find that obj.mon is an int, not a string.
Upvotes: 0