JSP没有将数据返回给JQuery AJAX [英] JSP not returning data to JQuery AJAX

查看:491
本文介绍了JSP没有将数据返回给JQuery AJAX的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我的功能。我试图从下面的JSP页面获取数据。两个文件都在同一个位置。我的错误是什么?



sample.js (包含在某个文件中):

  function getUnits(){
$ .ajax({

url:。 ./js/addunits.jsp,
成功:函数(returndata){
alert(returndata);
}
});
}

JSP页面 addunits.jsp

 <%
out.print(hi);
>%

在HTML页面中,我有一个选择列表。
更改后,将调用此函数 getunits

  $(#select)。change(function(){
getUnits();
var e = document.getElementById(select);
var SelValue = e.options [ e.selectedIndex] .text;
document.getElementById('crs')。innerHTML = SelValue;
});


解决方案

您忘了刷新缓冲区。

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

编辑:



如果你有ajax调用的成功状态代码,那么这是一个假设,并且它可能特别像你的情况,但并非在所有情况下,因为如果你使用了某些javascript 文件,那么你可能会犯同样的错误两次。在页面上构建一些URL时,不要在代码中使用相对路径,尤其是在从不同位置分派/包含页面时。接下来在绝对路径中,您应该包含上下文路径 $ {pageContext.request.contextPath} 或使用JSTL的< c:url> 标签。您可以这样做来加载 sample.js 但不能在其中,因为您只能在JSP页面上使用JSP内容。因此,您可以在JSP中构建URL并将其作为参数传递给js函数,例如



sample.js :(包含在某些文件中)

  function getUnits(theUrl){
$ .ajax({

url:theUrl,
success:function(returndata){
alert(returndata);
}
});
}

因此,在JSP页面中(你应该使用 jsp 你应该保留JSP页面的文件夹)使用

 < script> 
...
getUnits('$ {pageContext.request.contextPath} /jsp/addunits.jsp');
...
< / script>


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(included in some file):

function getUnits(){
 $.ajax({

url:"../js/addunits.jsp",
success: function(returndata){
    alert(returndata);
}
});
}

JSP Page addunits.jsp:

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

In a HTML page, I have a select list. On change, this function getunits will be called.

    $("#select").change(function() {
    getUnits();
    var e = document.getElementById("select");
    var SelValue = e.options[e.selectedIndex].text;
    document.getElementById('crs').innerHTML = SelValue;
}); 

解决方案

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 <c:url> 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

<script>
  ...
  getUnits('${pageContext.request.contextPath}/jsp/addunits.jsp');
  ...
</script>

这篇关于JSP没有将数据返回给JQuery AJAX的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆