为什么jQueryAjax调用返回未定义的对象?
我对php服务器脚本进行了jquery ajax调用,但遇到一条错误消息,告诉我返回的JSON类型的数据没有定义。 那么,如何解决这个问题呢?以下是jquery ajax调用:为什么jQueryAjax调用返回未定义的对象?,jquery,json,ajax,Jquery,Json,Ajax,我对php服务器脚本进行了jquery ajax调用,但遇到一条错误消息,告诉我返回的JSON类型的数据没有定义。 那么,如何解决这个问题呢?以下是jquery ajax调用: $.ajax({ url: 'listWorkProcess.php', method: 'post', data: data, success: function(feedback){
$.ajax({
url: 'listWorkProcess.php',
method: 'post',
data: data,
success: function(feedback){
$.parseJSON('feedback');
console.log(feedback);// loged correctly
alert(feedback.message);//here is the undefined message
$('#table').html(feedback.row_html);//this not executed Why?
}
});//end of ajax
您只是将一个字符串传递给parseJSON。 您需要将其分配给某个对象,并使用您获得的数据,如:
success: function(feedback){
var data = $.parseJSON(feedback);
console.log(data);// loged correctly
alert(data.message);//here is the undefined message
$('#table').html(data.row_html);//this not executed Why?
}
您只是将一个字符串传递给parseJSON。 您需要将其分配给某个对象,并使用您获得的数据,如:
success: function(feedback){
var data = $.parseJSON(feedback);
console.log(data);// loged correctly
alert(data.message);//here is the undefined message
$('#table').html(data.row_html);//this not executed Why?
}
尝试以下AJAX代码:
在您的代码中,需要进行一些小的更改,如下面的AJAX代码中包含“dataType='json'”
$.ajax({
url: 'listWorkProcess.php',
dataType:"json",
method: 'post',
data: data,
success: function(feedback){
console.log(feedback);// loged correctly
alert(feedback.message);//here is the undefined message
$('#table').html(feedback.row_html);//this not executed Why?
}
});
这是获取响应的简单方法,该响应以JSONECODE格式设置在控制器中,只需将数据类型:“json”指定给AJAX代码即可。尝试以下AJAX代码:
在您的代码中,需要进行一些小的更改,如下面的AJAX代码中包含“dataType='json'”
$.ajax({
url: 'listWorkProcess.php',
dataType:"json",
method: 'post',
data: data,
success: function(feedback){
console.log(feedback);// loged correctly
alert(feedback.message);//here is the undefined message
$('#table').html(feedback.row_html);//this not executed Why?
}
});
这是获得响应的简单方法,响应以JSONECODE格式设置在控制器中,只需将数据类型:“json”指定给AJAX代码。问题是:在尝试访问一个json元素时,返回的json未定义,即使使用了jquery解析函数!!!您可以提供控制台中得到的响应的屏幕截图吗?问题是:当尝试访问一个JSON元素时,返回的JSON是未定义的,即使使用了jquery解析函数!!!您可以提供控制台中得到的响应的屏幕截图吗?简单地设置
数据类型:'json'
,然后不需要对其进行解析,这将在内部完成,重要的是,任何无效的json都将触发错误句柄简单地设置数据类型:'json'
,然后不需要对其进行解析,将在内部完成,重要的是,任何无效的json都将触发错误处理程序