0

Can I submit a request through jquery using $.get and use the returned result as it returns from normal submition to server,

here's what I mean, I want to make page navigator for all users in the system, I want to show 10 users per page, for the first time I request the page, the servlet returns 10 users and I show then like this

 <c:forEach items="${requestScope.AllUsers}" var="user" varStatus="loop">
        <tr class="userRow">
            <td class="numberWidth">${loop.index + 1}</td>
            <td class="nameWidth user" id="${user.userno}">${user.fullName}</td>
            <td>
                <c:choose>
                    <c:when test="${user.active == true}">
                        <button name="DeactivateBtn"  id="deactivate" class="deactivate btn">تعطيل</button>
                    </c:when>
                    <c:when test="${user.active == false}">
                        <button name="ActivateBtn" id="activate" class="activate btn">تفعيل</button>
                    </c:when>
                </c:choose>
            </td>
        </tr>
    </c:forEach>

when user click on the next page button, I sent request to servlet through jQuery

var param = {
    "page": "ViewUsers",
    "from": page_index * items_per_page,
    "to": (page_index * items_per_page) + items_per_page
};
$.get("InstitutionManagementServlet",param);

and it calls the same method that returns different users depending on the range I sent, the problem is that, for second request to servlet the users are not appear as the first time, even in consol shows that the return list has users values?

Does that have a concern of sending request using jquery???

gilly3
  • 87,962
  • 25
  • 144
  • 176
palAlaa
  • 9,500
  • 33
  • 107
  • 166

1 Answers1

0

Here,

$.get("InstitutionManagementServlet",param);

You are not handling the response of the request at all. A callback function is missing. So whatever the servlet is returning is totally ignored. You need to add a callback function to handle the response:

$.get("InstitutionManagementServlet", param, function(response) {
    // Handle response here. E.g., insert retrieved data in the desired place.
});

See also:


Update: so, you actually want to send a synchronous GET request using JavaScript (e.g. changing the current page location altogether)? You can use window.location for this.

window.location = "InstitutionManagementServlet?" + $.param(param);

See also:

Community
  • 1
  • 1
BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
  • I don't want to handle it in jquery, I think I can handle it as the same way as it returns from servlet, am I correct?? – palAlaa Sep 02 '11 at 19:31
  • So you don't want to use Ajax? Then throw all that jQuery away and just use a normal `
    ` with a normal `` button. The extra parameters can be sent as `` fields. I however question how that makes sense. Do you want to support JS-disabled clients as well or something?
    – BalusC Sep 02 '11 at 19:32
  • No I don't want to use ajax, but the page naviagtor is created usin jQuery API, so I can't submit the request as a normal submit button! – palAlaa Sep 02 '11 at 19:33
  • Then let jQuery create a HTML form and submit it or call `window.location`, or just don't create page navigator in jQuery, but using JSP/HTML. I'd however reconsider your aversion against Ajax. – BalusC Sep 02 '11 at 19:34
  • No, I don't avert using AJAX, but I want to find a way to not rework the existing code when using jquery. – palAlaa Sep 02 '11 at 19:37
  • If your servlet indeed returns a full blown HTML page, then just change the `window.location`. See updated answer. – BalusC Sep 02 '11 at 19:42