jQuery JSON将时间值转换为数字值

时间:2018-12-07 15:13:50

标签: javascript jquery

我在通过jQuery AJAX进行JSON转换时遇到问题。

Ajax设置

$.ajax({
  url: 'ajax/get_available_schedule.php',
  type: 'GET',
  dataType: 'json',
  data: {field: field, day: day, hour: inicio},
})

我收到服务器的回复

{"success":true,"ranges":[["22:00","22:30"],["22:30","23:00"],["01:30","02:00"],["02:00","02:30"]],"close":"03:30"}

但是现在我在Ajax成功中对响应进行“ console.log”,它将转换为:

close: "03:30"
ranges: Array(2)
0: (2) [5400, 9000]
1: (2) [79200, 82800]
success: true

我不知道为什么将'ranges'数组转换为数字,我希望这可能是响应,一个由2个字符串组成的数组

我尝试用Google搜索问题,但是什么也找不到,或者我不知道如何搜索。

2 个答案:

答案 0 :(得分:1)

我知道我已经看到了它的副本,但是目前找不到,所以去了。

var data = { ranges: [ "A", "B", "C" ] };

//log the data element to the console
console.log( data );

//set the anotherVariable to point to the ranges array
//this does NOT create a separate element
//both variables point to the same array in memory
var anotherVariable = data.ranges;

//this changes both variables, because as said before, they point to the
//same array
anotherVariable[1] = "weee";

/*

If you look at your actual browser, you will see that the consoled
data has ranges of "A", "weee", "C".  This is because your browser
is showing you the value of the element, up to date.  Not only of
when you logged it.

*/

答案 1 :(得分:1)

问题是在代码中稍后声明的disabled_hours库选项中的timepicker var。

它会更改disabled_hours的值,直到更改后我才能在控制台中查看。

$.ajax({
  url: 'ajax/get_available_schedule.php',
  type: 'GET',
  dataType: 'json',
  data: {field: field, day: day, hour: inicio},
})
.done(function(dataSCHEDULE) {
  console.log(dataSCHEDULE);
  // debugger;
  if (dataSCHEDULE.success) {
    disabled_hours  = dataSCHEDULE.ranges;
    console.log(disabled_hours);
    close_hour      = dataSCHEDULE.close;
  }
  else {
    console.warn('API ERROR -> Getting available hours');
  }
})
.fail(function(error) {
  console.error('API ERROR -> Getting available hours');
  console.log(error);
});