下面是代码片段。我曾经使用 AJAX 从 html 中获取一些属性并将其传递给服务器。 AJAX 成功后,我需要在 Html 页面上返回一些内容。
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);
}
});
是否可以使用jquery get
方法动态更改HTML
。
因为下面的脚本不起作用
$("#rate").html("Ratings : "+rating);
$("#rate").show('slow');
最佳答案
尝试将 ajax 调用的 dataType
定义为 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);
}
});
关于javascript - 如何在ajax中使用jquery get方法动态更改html,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31453108/