2

I have a jsp page which displays the details of a student .Based on the student selection from the dropdown box on change event will be fired and retrieve the min and max marks for the student.

<form name="listBean"> 
    <c:forEach var="Item" items="${listBean.nameList}" varStatus="status">
        <input type="number"name="nameList<c:outvalue='[${status.index}]'/>.initialMarks"/> 
        <input type="number" name="nameList<c:out value='[${status.index}]'/>.finalMarks"/> 
        <input type="submit" value="submit" id="submit" />
        MinMarks:<c:out value="${Item.minMarks}"/></c:if> 
        MaxMarks:<c:out value="${Item.maxMarks}"/></c:if>
     </c:forEach>
</form>

After retrieval ,updated data will be stored into the bean.Server request is handled using jquery.ajax() method

function onChange() {
    jQuery('form').each(function() {
        jQuery.ajax({
            url: "http://localhost:9001/submitStudent.do?requestType=auto",
            data: $('form').serialize(),
            type: 'POST'
          }); 
          location.reload();
    }); 
}

Once the server response is successful , i will be reloading the page so that the page will be refreshed with the bean data set during the ajax call.

But it is not displaying the data?What i am doing wrong ?

Or is there any better solution to achieve this?

Any suggestions are welcome .

Thanks

jaggs
  • 298
  • 2
  • 9
  • 28
  • but you do see the `alert()` after the request? – roullie May 12 '15 at 03:58
  • No I am not seeing any alerts.Modified my code .. – jaggs May 12 '15 at 04:01
  • 3
    What is the point of using Ajax if you reload the page? Also your Ajax calls smell of wishful thinking. I do not see where you assign the change event and I do not see you use the success or .done event – mplungjan May 12 '15 at 04:04
  • @mplungjan I got your point.Yes the ultimate use of ajax calls doesn't serve any purpose here.Is the same result can be achieved without page reload ? – jaggs May 12 '15 at 04:14
  • Just post the form without further ado. Do you even have more than one form on the page and have they all been changed. I think you need a developer to take a look at your sequence – mplungjan May 12 '15 at 05:26

1 Answers1

0

It looks like you are reloading the page immediately after sending the AJAX request, potentially before the server has received and processed it.

You could store your ajax requests in an array and only reload the page when all requests have completed using jquery's when().

function onChange() {
    var requests = [];

    jQuery('form').each(function() {
        requests.push(jQuery.ajax({
            url: "http://localhost:9001/submitStudent.do?requestType=auto",
            data: $('form').serialize(),
            type: 'POST'
        }));
    });

    jQuery.when.apply(jQuery, requests).then(location.reload, errHandler);
}

function errHandler (err) {
    // handle any errors
}
Alex McMillan
  • 17,096
  • 12
  • 55
  • 88