javascript - 如何在ajax中使用jquery get方法动态更改html

标签 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':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/

相关文章:

javascript - 如何从一个实例中创建一个新的类实例?

javascript - 地形交叉性能

javascript - jQuery - 单击一个元素会触发另一个元素

javascript - 两个圆圈应该跟随光标——一大一小(有延迟)

html - 使用 bootstrap 创建标题图像

html - CSS - 溢出 : hidden not working on img

javascript - CSS 'translate(0px, 0px)' 元素样式的正则表达式匹配值

javascript - 从 javascript 从 highcharts 中的线/散点图中获取工具提示内容

javascript - DragDealer slider javascript 小部件 - 要在页面中嵌入哪些文件?

jQuery在点击时改变div的大小