我正在尝试使用JSF primefaces xhtml文件中的javascript创建$ .ajax请求。返回的数据是来自primefaces的模板页面。 StockServlet未在服务器上执行。我相信它正被Faces servlet捕获。以前有人有这个问题吗?
<script>
function getCubeData(){
$cubeName = $("#form\\:cubeName").val();
// alert("ajax call"+$cubeName);
$.ajax({
url: 'StockServlet',
dataType: 'json',
data: {cubeName: $cubeName},
type: 'get',
cache: false,
success: function(response)
{alert("success"+response);
var infoHTML = '';
$.each(response, function(stock, stockInfo)
{
infoHTML += '<p>Symbol: ' + stock + " Company: " + stockInfo.name + ' Price: ' + stockInfo.price + '</p>';
})
$("#mycube2").innerHTML(infoHTML);
alert("infohtml"+infoHTML);
},
error: function(request, textStatus, errorThrown)
{
alert("error:" + textStatus);
},
complete: function(request, textStatus)
{
alert("complete" + request.responseText);
alert("complete" + textStatus);
}
});
}
我的StockServlet在这里定义
@WebServlet(asyncSupported = false,name =“StockServlet”,urlPatterns = {“/ StockServlet”}) 公共类StockServlet扩展了HttpServlet ........
答案 0 :(得分:1)
HTML文档中的所有相对URL及其中的所有脚本都与HTML文档本身的URL相关。即您在浏览器的地址栏中看到的网址。您用于servlet url: 'StockServlet'
的URL不以域或斜杠开头,因此相对于文档URL中的当前文件夹。显然,您的JSF页面本身就位于子文件夹中,因此servlet的相对URL是完全错误的。它根本不在该子文件夹中。
你基本上需要去一个文件夹:
url: '../StockServlet'
或者使其与域相关(以/
开头):
url: '#{request.contextPath}/StockServlet'
无关具体问题,如果您已在FacesServlet
这样的子文件夹网址格式上映射/faces/*
,那么您最好考虑重新映射它到*.xhtml
。另请参阅JSF Facelets: Sometimes I see the URL is .jsf and sometimes .xhtml. Why?