Javascript 如何在ajax中使用jQueryGet方法动态更改html
下面是代码片段。我曾经使用AJAX从html获取一些属性并将其传递给服务器。AJAX成功后,我需要返回Html页面上的一些内容Javascript 如何在ajax中使用jQueryGet方法动态更改html,javascript,jquery,html,ajax,Javascript,Jquery,Html,Ajax,下面是代码片段。我曾经使用AJAX从html获取一些属性并将其传递给服务器。AJAX成功后,我需要返回Html页面上的一些内容 var hotel=$( "#listHotel option:selected" ).val(); $.ajax({ url: "/getHotels", contentType: "application/xml; charset=utf-8", data: {'hotel':hot
var hotel=$( "#listHotel option:selected" ).val();
$.ajax({
url: "/getHotels",
contentType: "application/xml; charset=utf-8",
data: {'hotel':hotel},
type: "GET",
success: function(response){
var r= JSON.parse(response);
var rating =r.message
alert(rating);
$("#rate").html("Ratings : "+rating);
$("#rate").show('slow');
console.log(rating);
},
error: function(error){
alert(response);
console.log(error);
}
});
是否可以使用jqueryget
方法动态更改HTML
。
因为下面的脚本不起作用
$("#rate").html("Ratings : "+rating);
$("#rate").show('slow');
尝试将ajax调用的
数据类型定义为json
:
var hotel=$( "#listHotel option:selected" ).val();
$.ajax({
url: "/getHotels",
contentType: "application/xml; charset=utf-8",
data: {'hotel':hotel},
type: "GET",
dataType: "json",
success: function(response){
var r= response;
var rating =r.message
alert(rating);
$("#rate").html("Ratings : "+rating);
$("#rate").show('slow');
console.log(rating);
},
error: function(error){
alert(response);
console.log(error);
}
});
... 不工作对我们任何人来说都不足以帮助你。返回的数据是json
还是xml
?控制台日志(响应)
的输出是什么?你看到任何错误了吗?如果警报实际显示了正确的评级(并且你有一个元素,并且页面上只有一个元素id为rate
),那么应该是这样的。因为我正在使用get方法。在AJAX中,页面会自动重新加载,因此html会更改,但我仍然收到警报。它不起作用。AJAX调用完成后,页面开始加载,最后加载更改后的html$(“#rate”).html(“Ratings:+rating)代码>不再持久化