I was trying to populate collection(collection of items) from servlets to my jsp page..
In my servlet code I store the items in collection:
String itemsJson = new Gson().toJson(items);
response.setContentType("application/json");
response.setCharacterEncoding("UTF-8");
response.getWriter().write(itemsJson);
In my jsp page I am tryin to populate the items onto a jquery resizable box.The code is as follows:
$(document).ready(function() {
$("#resizable").resizable();
$.getJSON('resizable', function(itemsJson) {
var $resizable = $('#resizable');
$.开发者_如何转开发each(itemsJson, function(index, itemcatalog) {
$('<option>').text(itemcatalog.item).appendTo($resizable);
});
});
});
<div class="demo">
<div id="resizable" class="ui-widget-content">
<h3 class="ui-widget-header">Item List</h3>
<option></option>
Then the URL is plain wrong. You're using a relative URL in $.getJSON()
$.getJSON('resizable', function(itemsJson) {
Imagine that this code is been placed in a JSP file which is opened by the following URL
http://localhost:8080/context/pages/page.jsp
then you need to ensure that the servlet is available on
http://localhost:8080/context/pages/resizable
JavaScript/jQuery will namely map relative URLs to the document's base URL.
Or when the servlet is actually listening on
http://localhost:8080/context/resizable
(test it by opening its address straight in browser address bar)
then you need to change the $.getJSON()
URL as follows
$.getJSON('../resizable', function(itemsJson) {
or (domain-relative)
$.getJSON('/context/resizable', function(itemsJson) {
or (works only if this is inside JSP)
$.getJSON('${pageContext.request.contextPath}/resizable', function(itemsJson) {
or to move the JSP file to
http://localhost:8080/context/page.jsp
Or if the URL is actually correct, then it simply means that the servlet failed to startup. Read the server logs for the exception/stacktrace and fix the code accordingly. Or maybe the servlet is not mapped on an URL at all.
精彩评论