Heather Heather - 6 months ago 32
Javascript Question

Why does my integer object automatically convert to string after post method?

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 = {

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 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) {
$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:
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.


I was able to resolve my issue with the following:

      $rootScope.edit.fullDay[key] = parseInt($rootScope.edit.fullDay[key]);