[英]How can I parse specific parts of my AJAX array in Javascript?
I used AJAX to send back an array with many variables worth of data to my page to have displayed. 我使用AJAX将包含许多变量数据的数组发送回要显示的页面。 The array looks something like this right now:
该数组现在看起来像这样:
{"dateName":"05/18/2016","hour1":null,"hour2":null,"hour3":null,"hour4":null,"hour5":null,"hour6":null,"hour7":null,"hour8":null,"hour9":null,"hour10":null,"hour11":null,"hour12":null,"hour13":null,"hour14":null,"hour15":null,"hour16":null,"hour17":null,"hour18":null,"hour19":null,"hour20":null,"hour21":null,"hour22":null,"hour23":null,"hour24":null}
Now I am displaying the parts of the array in my data boxes using 现在,我使用以下命令在数据框中显示数组的各个部分
//AJAX Data
success: function(data) {
var array = data.split(",");
$("#date").html(array[0]);
i = 0;
while (i < 25) {
$("#hour"+i).html(array[i]);
i++;
}
This displays data that looks like this on my webpage 这会在我的网页上显示如下所示的数据
"hour1":"sleep"
“ hour1”:“睡觉”
As you can see, the variable name in quotes and the variable value that was passed through ajax. 如您所见,变量名用引号引起来,并且变量值是通过ajax传递的。 But I only want
但是我只想要
sleep
睡觉
displayed (no quotes, no variable) . 显示(无引号,无变量)。 How do I get the variable name and quotes out of my displayed data?
如何从显示的数据中获取变量名称和引号?
Thank you so much! 非常感谢!
//AJAX Data
success: function(data) {
var hour = "hour";
for(var i=0; i<data.length; i++) {
hour += i;
console.log(data.hour)
}
success: function(data) {
var json_obj =JSON.parse(data)
$("#date").html(json_obj['datename']);
i = 0;
while (i < 25) {
$("#hour"+i).html(json_obj['hour'+i]);
i++;
}
}
use this 用这个
$.ajax({url: "demo_test.txt", success: function(result){
// $("#div1").html(result);
var parsed = JSON.parse(result);
alert(parsed["hour1"]);
}});
Through the help of a few of you awesome members here at stackexchange I have finally figured out how to send an ajax, call back multiple variables, and arrange them cleanly on my page. 通过在stackexchange的一些出色成员的帮助下,我终于弄清楚了如何发送ajax,调用多个变量并将其整齐地排列在我的页面上。 Attached is my code, I hope that this can help future users.
附件是我的代码,希望对以后的用户有所帮助。
//SEND MULTIPLE DATA THROUGH AJAX
<script>
function ajax() {
$.ajax({
type: "POST",
url: "changeDate.php",
data: {
amount: changeDate,
loginName: "benjamin_lawson"
},
success: function(result) {
//make array out of data
var array = JSON.parse(result);
$("#date").html(array[0]);
i = 0;
while (i < 25) {
//call array information for parts
$("#hour"+i).html(array[i]);
i++;
}
}
});
}
</script>
<?php
//create the array
$response_arr = array();
//add your variables to the array
$response_arr[] = date("m/d/Y", strtotime("+" . $amount . " day"));
$response_arr[] = $row[$dateName];
//echo the final array to be sent to your ajax
echo json_encode($response_arr);
?>
You can use JSON.parse to convert the string to json obj : 您可以使用JSON.parse将字符串转换为json obj:
json_obj=JSON.parse(data) $("#date").html(json_obj['']);
json_obj = JSON.parse(data)$(“#date”)。html(json_obj ['']);;
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.