[英]Display data from json array using ajax
I have the following code on index page the script contains part of the code that will call the data from test page 我在索引页上有以下代码,脚本包含部分代码,这些代码将从测试页中调用数据
<div class="leftbox" id="proddisplay">
</div>
var onSubmit = function(e) {
var txtbox = $('#txt').val();
var hiddenTxt = $('#hidden').val();
$.ajax({
type: 'post',
url: 'test.php',
data: {
txt: txtbox,
hidden: hiddenTxt
},
cache: false,
success: function(returndata) {
$('#proddisplay').html(returndata);
console.log(returndata);
},
error: function() {
console.error('Failed to process ajax !');
}
});
};
From test.php i am getting json array that looks like this 从test.php我正在获取看起来像这样的json数组
[1,2,"text","text2"]
I want to display the json array data in a tabular form and display it inside the div. 我想以表格形式显示json数组数据并将其显示在div中。 the view of table should be something like this (it will have some css of its own)
表的视图应该是这样的(它将有一些自己的CSS)
static text: 1
static text: 2
static text: text
static text: text2
static text will be given by me and remains the same throughout however the values of array would change. 静态文本将由我提供,并且在整个过程中保持不变,但是array的值将更改。 can anyone tell how i can do so
谁能告诉我该怎么做
individually i can display the data from json, but here i also need to put json data in a table and then put that table within a div 我可以单独显示json中的数据,但是在这里我还需要将json数据放入表中,然后将该表放入div中
First of all you need to encode the array in php before sending to frontend, use json_encode() and then decode it in the success function using JSON.parse() . 首先,您需要先在php中对数组进行编码,然后再发送到前端,使用json_encode(),然后使用JSON.parse()在成功函数中对其进行解码。 After just run a loop throught the array.
刚在阵列中运行一个循环之后。
var onSubmit = function(e) {
var txtbox = $('#txt').val();
var hiddenTxt = $('#hidden').val();
$.ajax({
type: 'post',
url: 'test.php',
data: {
txt: txtbox,
hidden: hiddenTxt
},
cache: false,
success: function(returndata) {
var returneddata = JSON.parse(returndata);
var htmlData= '';
for(var i=0; i<returneddata.length; i++){
htmlData+= 'static text: '+returneddata[i]+' <br>';
}
$('#proddisplay').html(htmlData);
console.log(returndata);
},
error: function() {
console.error('Failed to process ajax !');
}
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.