Javascript ajax post方法错误
在下面的示例中,我能够传递国家列表的JSON表示并显示它,但我无法显示消息“找不到任何国家”。请检查下面的代码: 控制器:Javascript ajax post方法错误,javascript,jquery,ajax,json,Javascript,Jquery,Ajax,Json,在下面的示例中,我能够传递国家列表的JSON表示并显示它,但我无法显示消息“找不到任何国家”。请检查下面的代码: 控制器: if ($this->model_abc->did_get_country_list($user_id)) { $country["results"]= $this->model_abc->did_get_country_list($user_id); echo j
if ($this->model_abc->did_get_country_list($user_id)) {
$country["results"]= $this->model_abc->did_get_country_list($user_id);
echo json_encode($country);
}
else {
$country = array('message' => "Could not find any countries" );
echo json_encode($country);
}
JS文件:
$.post('cont/get_country_list', function (country) {
$.each(country.results, function(i, res) {
var item = $('<div>'),
title = $('<h3>');
title.text(res);
item.append(title);
item.appendTo($("#country_list_is_got"));
});
},
"json").error(function() {
$( "#country_list_is_not_got").text(country.message);
});
$.post('cont/get_country_list', function (country) {
if (country.message !== undefined) {
$( "#country_list_is_not_got").text(country.message);
} else {
$.each(country.results, function(i, res) {
var item = $('<div>'),
title = $('<h3>');
title.text(res);
item.append(title);
item.appendTo($("#country_list_is_got"));
});
}
});
$.post('cont/get\u country\u list',函数(country){
美元。每个(国家/地区、结果、功能(i、res){
变量项=$(''),
标题=$('');
标题.文本(res);
项目.附加(标题);
项目.附件($(“#国家/地区列表已获得”);
});
},
“json”)。错误(函数(){
$(“#country_list_is_not_got”).text(country.message);
});
更新:
我按以下方式更改了代码,现在从控制台收到了此错误:未捕获类型错误:无法读取未定义的属性“length”
JS文件:
$.post('cont/get_country_list', function (country) {
$.each(country.results, function(i, res) {
var item = $('<div>'),
title = $('<h3>');
title.text(res);
item.append(title);
item.appendTo($("#country_list_is_got"));
});
},
"json").error(function() {
$( "#country_list_is_not_got").text(country.message);
});
$.post('cont/get_country_list', function (country) {
if (country.message !== undefined) {
$( "#country_list_is_not_got").text(country.message);
} else {
$.each(country.results, function(i, res) {
var item = $('<div>'),
title = $('<h3>');
title.text(res);
item.append(title);
item.appendTo($("#country_list_is_got"));
});
}
});
$.post('cont/get\u country\u list',函数(country){
如果(country.message!==未定义){
$(“#country_list_is_not_got”).text(country.message);
}否则{
美元。每个(国家/地区、结果、功能(i、res){
变量项=$(''),
标题=$('');
标题.文本(res);
项目.附加(标题);
项目.附件($(“#国家/地区列表已获得”);
});
}
});
在没有找到国家/地区的情况下,您没有设置错误回复代码,因此jQuery不会将其视为错误
else {
header("HTTP/1.1 500 Error");
$country = array('message' => "Could not find any countries" );
echo json_encode($country);
}
此外,错误函数需要从其参数获取数据:
.error(function(xhr) {
country = $.parseJSON(xhr.responseText);
$( "#country_list_is_not_got").text(country.message);
});
只有当服务器响应500-HTTP代码(或404、403或超时等)时,才会调用error函数。您返回的只是带有正常HTTP状态代码的有效JSON,因此调用success函数。在该成功功能中,您应该检查是否有消息:
$.post('cont/get_country_list', function (data) {
if (data.message !== undefined) {
$( "#country_list_is_not_got").text(data.message);
} else {
$.each(country.results, function(i, res) {
var item = $('<div>'),
title = $('<h3>');
title.text(res);
item.append(title);
item.appendTo($("#country_list_is_got"));
});
}
});
$.post('cont/get\u country\u list',函数(数据){
if(data.message!==未定义){
$(“#国家/地区列表(未)”).text(data.message);
}否则{
美元。每个(国家/地区、结果、功能(i、res){
变量项=$(''),
标题=$('');
标题.文本(res);
项目.附加(标题);
项目.附件($(“#国家/地区列表已获得”);
});
}
});
那么什么是$user\u id
?感谢您的帮助。我改变了我的问题。您能检查一下吗?看起来像是拙劣的国家/地区。消息
和国家/地区。结果
未定义。您是否在web developer工具/控制台(F12)中检查了该JSON请求的服务器响应?我得到了以下结果:{“结果”:[“美国”,“加拿大”]},其他{“消息”:“找不到任何国家”。}