Jquery $.post和$.ajax之间的jQM差异,我发现$.post有错误
在我的混合应用程序(使用jQM作为框架)中,我尝试从服务器检索数据 我尝试了$.post和$.ajax方法 使用$.ajax,我可以使用数据[0].name“访问返回数据 使用$.post和完全相同的返回数据,当我尝试使用数据[0].name“访问数据时,我得到了未定义的数据 我的代码($.ajax)并显示正确的数据:Jquery $.post和$.ajax之间的jQM差异,我发现$.post有错误,jquery,ajax,jquery-mobile,Jquery,Ajax,Jquery Mobile,在我的混合应用程序(使用jQM作为框架)中,我尝试从服务器检索数据 我尝试了$.post和$.ajax方法 使用$.ajax,我可以使用数据[0].name“访问返回数据 使用$.post和完全相同的返回数据,当我尝试使用数据[0].name“访问数据时,我得到了未定义的数据 我的代码($.ajax)并显示正确的数据: $(document).on('pagebeforeshow', '#restaurantList', function() { $.ajax({ url: "h
$(document).on('pagebeforeshow', '#restaurantList', function() {
$.ajax({
url: "http://mydomain.com/api/restaurant/allstate/allcuisine",
type: "POST",
dataType: "json",
success: function (data) {
alert(data[0].restaurant_id);
}
});
});
$(document).on('pagebeforeshow', '#restaurantList', function() {
$.post("http://mydomain.com/api/restaurant/allstate/allcuisine",
function(data){
alert(data[0].restaurant_id);
});
});
我的代码不起作用($.post)并给出“未定义”:
$(document).on('pagebeforeshow', '#restaurantList', function() {
$.ajax({
url: "http://mydomain.com/api/restaurant/allstate/allcuisine",
type: "POST",
dataType: "json",
success: function (data) {
alert(data[0].restaurant_id);
}
});
});
$(document).on('pagebeforeshow', '#restaurantList', function() {
$.post("http://mydomain.com/api/restaurant/allstate/allcuisine",
function(data){
alert(data[0].restaurant_id);
});
});
为什么呢?我需要使用$是有原因的。post但我无法访问数据。我检查了返回的JSON,两个方法返回的数据完全相同
请告诉我这两者之间的区别,以及为什么我从$中得到未定义的。谢谢。获得$。post可正确处理JSON返回数据。必须指定数据类型 根据jQM文件: dataType类型:字符串服务器预期的数据类型。 默认值:智能猜测(xml、json、脚本、文本、html) 但这是聪明的猜测似乎不够聪明 为此,请添加“json”,示例如下:
$(document).on('pagebeforeshow', '#restaurantList', function() {
$.post("http://mydomain.com/api/restaurant/allstate/allcuisine",
function(data){
alert(data[0].restaurant_id);
}, "json");
});
有不同的默认值通过
$.post
传递给$.ajax
,因此您需要完整显示ajax代码$.post
是$.ajax的简写({type:“post”,url:url,data:data,success:success,dataType:dataType})代码>还可以使用免费工具(如Fiddler2)检查返回数据,因为您可能只是得到了一个服务器错误。您好@TrueBlueAusie,谢谢您的评论。我已经添加了我测试的示例代码。请提供建议,谢谢。您是否检查了流量(使用F12 Chrome调试工具或安装Fiddler2)?流量表示入站数据?我正在使用英特尔的XDK开发工具。它有内置的调试工具。入站数据,是的。您需要查看响应是否不同(不默认为json
etc)。