Javascript 使用AJAX函数访问JSON元素
我需要从codeigniter控制器获取数据,并将其作为ajax调用发送到视图。所以我使用这个函数将数据发送到ajax函数Javascript 使用AJAX函数访问JSON元素,javascript,php,jquery,json,ajax,Javascript,Php,Jquery,Json,Ajax,我需要从codeigniter控制器获取数据,并将其作为ajax调用发送到视图。所以我使用这个函数将数据发送到ajax函数 function vendorAccess(){ $result = $this->Admin_model->allvendor(); foreach($result as $row){ $data = $row["joinedDate"]; $now = time(); $your_dat
function vendorAccess(){
$result = $this->Admin_model->allvendor();
foreach($result as $row){
$data = $row["joinedDate"];
$now = time();
$your_date = strtotime($data);
$datediff = $now - $your_date;
$cont = round($datediff / (60 * 60 * 24));
if($cont == 85){
$response['vendorName'] = $row['vendorName'];
echo json_encode($response);
}
}
}
这将向我的ajax函数发送数据。我的ajax功能如下
$(document).ready(function(){
$.ajax({
url: '<?php echo base_url();?>/Admin/vendorAccess',
type: 'get',
dataType:'text',
success: function(res){
console.log(res);
}
});
});
$(文档).ready(函数(){
$.ajax({
url:“/Admin/vendoracess”,
键入:“get”,
数据类型:'text',
成功:功能(res){
控制台日志(res);
}
});
});
使用这段代码,我得到如下输出
{“vendorName”:“肉桂大酒店”}{“vendorName”:“沙龙酒店”}
现在,我需要将这两个vendorName作为单独的两个名称使用jquery输入到我的div中。我在stackoverflow上搜索了这个问题,并找到了一些解决方案。但这些解决方案对我不起作用。一个例子是贝娄
$(document).ready(function(){
//$('#numOfNot').html('New');
var tmp=null;
$.ajax({
url: '<?php echo base_url();?>/Admin/vendorAccess',
type: 'get',
dataType:'json',
success: function(res){
alert(res.vendorName);
}
});
});
$(文档).ready(函数(){
//$('#numoffot').html('New');
var-tmp=null;
$.ajax({
url:“/Admin/vendoracess”,
键入:“get”,
数据类型:'json',
成功:功能(res){
警报(res.vendorName);
}
});
});
当我使用这个时,给我未定义的警告。
非常感谢您花费宝贵的时间来解决我的问题您需要在控制器中填充一个数组,而不是每次通过循环回显JSON
function vendorAccess(){
$result = $this->Admin_model->allvendor();
$names = array();
foreach($result as $row){
$data = $row["joinedDate"];
$now = time();
$your_date = strtotime($data);
$datediff = $now - $your_date;
$cont = round($datediff / (60 * 60 * 24));
if($cont == 85){
$names[] = $row['vendorName'];
}
}
echo json_encode(array('vendorName' => $names));
}
您需要在控制器中填充一个数组,而不是每次通过循环都回显JSON
function vendorAccess(){
$result = $this->Admin_model->allvendor();
$names = array();
foreach($result as $row){
$data = $row["joinedDate"];
$now = time();
$your_date = strtotime($data);
$datediff = $now - $your_date;
$cont = round($datediff / (60 * 60 * 24));
if($cont == 85){
$names[] = $row['vendorName'];
}
}
echo json_encode(array('vendorName' => $names));
}
我建议进行一些重构以使其正常工作。返回的JSON无效。您需要在
PHP
方法中创建一个对象数组,然后在foreach()
循环之外,echo
使用json\u encode()
方法将该数据返回到屏幕
PHP:
function vendorAccess(){
$result = $this->Admin_model->allvendor();
$vendors = [];
foreach($result as $row){
$data = $row["joinedDate"];
$now = time();
$your_date = strtotime($data);
$datediff = $now - $your_date;
$cont = round($datediff / (60 * 60 * 24));
if($cont == 85){
$vendors[] = ['vendorName' => $row['vendorName']];
}
}
echo json_encode($vendors);
}
$(document).ready(function() {
var tmp=null;
$.ajax({
url: '<?php echo base_url();?>/Admin/vendorAccess',
type: 'GET',
dataType:'json',
success: function(res) {
if(res && res.length) {
for(var a = 0, len = res.length; a < len; a++) {
console.log(res[a].vendorName);
}
}
}
});
});
然后,在JavaScript中,您需要检查以确保接收到一个数组,然后在数组中循环并以某种方式使用每个索引中的数据。在本例中,我使用console.log()
将数据记录到控制台,但您可以在此处执行任何其他逻辑:
JavaScript:
function vendorAccess(){
$result = $this->Admin_model->allvendor();
$vendors = [];
foreach($result as $row){
$data = $row["joinedDate"];
$now = time();
$your_date = strtotime($data);
$datediff = $now - $your_date;
$cont = round($datediff / (60 * 60 * 24));
if($cont == 85){
$vendors[] = ['vendorName' => $row['vendorName']];
}
}
echo json_encode($vendors);
}
$(document).ready(function() {
var tmp=null;
$.ajax({
url: '<?php echo base_url();?>/Admin/vendorAccess',
type: 'GET',
dataType:'json',
success: function(res) {
if(res && res.length) {
for(var a = 0, len = res.length; a < len; a++) {
console.log(res[a].vendorName);
}
}
}
});
});
$(文档).ready(函数(){
var-tmp=null;
$.ajax({
url:“/Admin/vendoracess”,
键入:“GET”,
数据类型:'json',
成功:功能(res){
if(res和res.length){
对于(var a=0,len=res.length;a
我建议进行一些重构以使其正常工作。返回的JSON无效。您需要在PHP
方法中创建一个对象数组,然后在foreach()
循环之外,echo
使用json\u encode()
方法将该数据返回到屏幕
PHP:
function vendorAccess(){
$result = $this->Admin_model->allvendor();
$vendors = [];
foreach($result as $row){
$data = $row["joinedDate"];
$now = time();
$your_date = strtotime($data);
$datediff = $now - $your_date;
$cont = round($datediff / (60 * 60 * 24));
if($cont == 85){
$vendors[] = ['vendorName' => $row['vendorName']];
}
}
echo json_encode($vendors);
}
$(document).ready(function() {
var tmp=null;
$.ajax({
url: '<?php echo base_url();?>/Admin/vendorAccess',
type: 'GET',
dataType:'json',
success: function(res) {
if(res && res.length) {
for(var a = 0, len = res.length; a < len; a++) {
console.log(res[a].vendorName);
}
}
}
});
});
然后,在JavaScript中,您需要检查以确保接收到一个数组,然后在数组中循环并以某种方式使用每个索引中的数据。在本例中,我使用console.log()
将数据记录到控制台,但您可以在此处执行任何其他逻辑:
JavaScript:
function vendorAccess(){
$result = $this->Admin_model->allvendor();
$vendors = [];
foreach($result as $row){
$data = $row["joinedDate"];
$now = time();
$your_date = strtotime($data);
$datediff = $now - $your_date;
$cont = round($datediff / (60 * 60 * 24));
if($cont == 85){
$vendors[] = ['vendorName' => $row['vendorName']];
}
}
echo json_encode($vendors);
}
$(document).ready(function() {
var tmp=null;
$.ajax({
url: '<?php echo base_url();?>/Admin/vendorAccess',
type: 'GET',
dataType:'json',
success: function(res) {
if(res && res.length) {
for(var a = 0, len = res.length; a < len; a++) {
console.log(res[a].vendorName);
}
}
}
});
});
$(文档).ready(函数(){
var-tmp=null;
$.ajax({
url:“/Admin/vendoracess”,
键入:“GET”,
数据类型:'json',
成功:功能(res){
if(res和res.length){
对于(var a=0,len=res.length;a
不能在循环中回显JSON。使用循环创建一个包含所有结果的数组,然后在循环完成后将其转换为JSON。@LGSonJSON.parse
不需要,dataType:'JSON'
会自动执行该操作。@Barmar是的……你是对的,错过了那一小部分:),在Javascript中,你需要循环res
,因为它将是一个数组,不是一个对象。@Barmar谢谢你,我知道我在循环时试图将其转换为json,然后我在两个数组中获取数据。你不能在循环中回显json。使用循环创建一个包含所有结果的数组,然后在循环完成后将其转换为JSON。@LGSonJSON.parse
不需要,dataType:'JSON'
会自动执行该操作。@Barmar是的……你是对的,错过了那一小部分:),在Javascript中,你需要循环res
,因为它将是一个数组,没有一个对象。@Barmar谢谢我知道我在循环时试图将其转换为json,然后我将数据分为两个数组谢谢@Barmar我将数据发送到我的ajax函数的正确方法谢谢@Barmar我将数据发送到我的ajax函数的正确方法谢谢$vendors[]=['vendorName'=>$row有一个小问题['vendorName'];如何更正这一行。我是否像$vendorName[]=$row['vendorName'];@D95抱歉,删除了尾随的]
结束方括号。谢谢你我对$vendorName[]=['vendorName'=>$row['vendorName']有点小问题;如何更正这一行。我是否像$vendorners[]=$row['vendorName']@D95抱歉,删除了尾随的]
结束方括号。