JSP not returning data to JQuery AJAX

前端 未结 1 1005
春和景丽
春和景丽 2021-01-16 04:20

Here is my function. I am trying to get the data from the JSP page below. Both files are at the same location. What is my mistake?

sample.js(inc

1条回答
  •  夕颜
    夕颜 (楼主)
    2021-01-16 04:51

    You forgot to flush a buffer.

    <%
     out.print("hi");
     out.flush();
    %> 
    

    EDIT:

    It was an assumption at the first place in case if you have a success status code for the ajax call and it might be in particular scenario like yours but not in all cases because if you used that javascript included in some file, then you might make the same mistake twice. When building some URL on the page don't use a relative path in the code, especially if the page is dispatched/included from different places. Next in the absolute path you should include a context path either ${pageContext.request.contextPath} or use JSTL's tag. You can do it for loading sample.js but not inside it because you can use the JSP stuff only on JSP page. So, you can build the URL in the JSP and pass it as parameter to js function like that

    sample.js:(included in some file)

    function getUnits(theUrl){
     $.ajax({
    
       url: theUrl,
       success: function(returndata){
         alert(returndata);
       }
     });
    }
    

    So, in JSP page (you should use jsp folder where you should keep JSP pages) use

    
    

    0 讨论(0)
提交回复
热议问题