Monday, May 20, 2024
 Popular · Latest · Hot · Upcoming
33
rated 0 times [  37] [ 4]  / answers: 1 / hits: 21006  / 10 Years ago, sat, december 27, 2014, 12:00:00

I just started learning Backbone JS. In below code, iam just trying to call url todos/1 expecting to get json data. Instead iam getting error in console as Failed to load resource: net::ERR_TOO_MANY_REDIRECTS I didnt understood why index.jsp keep redirecting many times itself.



Could anyone please explain me what went wrong and also solution too



test.jsp



<html>

<head>

<script
src=http://cdnjs.cloudflare.com/ajax/libs/jquery/2.1.1/jquery.min.js></script>
<script
src=http://cdnjs.cloudflare.com/ajax/libs/underscore.js/1.7.0/underscore-min.js></script>
<script
src=http://cdnjs.cloudflare.com/ajax/libs/backbone.js/1.1.2/backbone-min.js></script>


<script type=text/javascript>
var myname;
var myjson;

//WAY ONE
$.getJSON(todos/1, alertUser);

function alertUser(data) {
console.log(data.id);
id = data.id;
alert(id);

var TodoItem = Backbone.Model.extend({});
var todoItem = new TodoItem(data);

var TodoView = Backbone.View.extend({
render: function(){
var html = '<h3>' + this.model.get('description') + '</h3>';
$(this.el).html(html);
}});
var todoView = new TodoView({model : todoItem});
todoView.render();
console.log(todoView.el);

}


</script>

</head>


<body>

</body>


</html>


index.jsp



<%@ page language=java contentType=text/html; charset=UTF-8
pageEncoding=UTF-8%>
<%@page import=org.json.JSONObject %>


<%

JSONObject obj = new JSONObject();

obj.put(description, pick up milk);
obj.put(status, incomplete);
obj.put(id, 1);

response.setContentType(application/json);

out.print(obj);
%>


MyServlet.java



public class MyServlet extends HttpServlet {
private static final long serialVersionUID = 1L;

/**
* Default constructor.
*/
public MyServlet() {
// TODO Auto-generated constructor stub
}

/**
* @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
*/
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
response.sendRedirect(index.jsp);
}


}


web.xml



<?xml version=1.0 encoding=UTF-8?>
<web-app xmlns:web=http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd>
<servlet>
<servlet-name>MyServlet</servlet-name>
<servlet-class>MyServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>MyServlet</servlet-name>
<url-pattern>/todos/*</url-pattern>
</servlet-mapping>
</web-app>

More From » jquery

 Answers
5

I found a solution for my above problem. Hope it might somebody helps.



In MyServlet.Java I need to change below



response.sendRedirect(index.jsp);


to



response.sendRedirect(/MyProj/index.jsp);

[#68374] Wednesday, December 24, 2014, 10 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
janjadonb

Total Points: 4
Total Questions: 114
Total Answers: 118

Location: Mali
Member since Fri, Dec 3, 2021
3 Years ago
;