简体   繁体   English

在JSP页面中未执行来自使用Ajax的servlet响应的HTML

[英]HTML from servlet response using ajax is not being executed in JSP page

My problem is kinda weird. 我的问题有点奇怪。 I have a JSP page that calls servlet using JQuery/ajax on combobox change. 我有一个JSP页面,它在组合框更改时使用JQuery / ajax调用servlet。 Everything works fine, I get the response but html is displayed as text. 一切正常,我得到响应,但html显示为文本。 Other thing worth mentioning is when I call servlet directly by URL, everything is fine. 值得一提的另一件事是,当我直接通过URL调用servlet时,一切都很好。

Servlet response code: Servlet响应代码:

for(int i=0;i<tabstr.length;i++){
        wyjscie.println(i+": "+tabstr[i]+" <br>");
    }

JSP ajax call: JSP ajax调用:

$('#com2').change(function() {
                $.get('filtr', function(responseText) {
                    $('#result').text(responseText);
                });
            });

result is an ID of a DIV inside JSP page. 结果是JSP页面内DIV的ID。 I've done some servlets without ajax in the past and I didn't encounter this problem before. 我过去曾经做过一些没有ajax的servlet,而我以前从未遇到过这个问题。 Any idea how to deal with it? 知道如何处理吗?

您必须将其设置为html而不是text试试这个

$('#result').html(responseText);

I think your server doesn't specify the mime type of the response. 我认为您的服务器未指定响应的mime类型。 So you have to specify it or you can specify dataType in you ajax call. 因此,您必须指定它,也可以在ajax调用中指定dataType

$.ajax({
   url : "myUrl",
   dataType : "json",
   data : {
     param1 : value1,                   
   }
});

ajax api: Ajax API:

dataType : The type of data that you're expecting back from the server. dataType :您期望从服务器返回的数据类型。 If none is specified, jQuery will try to infer it based on the MIME type of the response... 如果未指定,则jQuery将尝试根据响应的MIME类型来推断它。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM