weixin_33682719 2014-08-08 10:47
浏览 25

JSP面临的AJAX问题

I am working on a small tool which just consists of a single JSP which is used for view as well as for processing the AJAX response.

If the call is of type 'GET', I am showing a form the user.

<form id="submitForm" method="post">
<div class="ui-widget">
<label for="tags">Please Select the Merchant : </label>
<input id="tags" name="mechant" style="width:300px;height:40px;border: 0.5px solid;border-radius: 5px;">&nbsp;&nbsp;
<input type="submit" value="Get Data" style="border: 0.5px solid;border-radius: 5px;">
</div>
</form>

And following is the code which will make the call.

$("#submitForm").submit(function() {
    $.ajax({
    url: 'serve_tx_2.jsp',
    type: 'POST',
    data: {q: $('#tags').val()},
    success: function(data) {
        $('#data').html(data);
        alert('Load was performed.');
    },
    beforeSend: function() {
       // $('.loadgif').show();
    },
    complete: function() {
       // $('.loadgif').hide();
    }
});
        //return false;
});

Once the user submits the form which goes as 'POST' the logic in the same JSP is returning the response.

Right now I am trying with a very simple logic.

response.setContentType("text/plain");  
response.setCharacterEncoding("UTF-8"); 
response.getWriter().write("Hello World");

Now when this response is return the whole of initial page is washed off and I just see "Hello World" on the page. Even though as per my understanding only the div with id "data" should be updated with value.

Kindly anyone have a look and let me know what might be going wrong here. Thanks in advance.

  • 写回答

1条回答 默认 最新

  • weixin_33717298 2014-08-08 13:47
    关注

    You could try preventing the default handler as well as prevent bubbling up the DOM.

    $("#submitForm").submit(function(event) {
        // Prevent the default action
        event.preventDefault();
        // Pevent propagation of this event up the DOM
        event.stopPropagation();
        $.ajax({
            url: 'serve_tx_2.jsp',
            type: 'POST',
            data: {q: $('#tags').val()},
            success: function(data) {
                $('#data').html(data);
                alert('Load was performed.');
            },
            beforeSend: function() {
                // $('.loadgif').show();
            },
            complete: function() {
                // $('.loadgif').hide();
            }
        });
        //return false;
    });
    
    评论

报告相同问题?

悬赏问题

  • ¥20 idea运行测试代码报错问题
  • ¥15 网络监控:网络故障告警通知
  • ¥15 django项目运行报编码错误
  • ¥15 请问这个是什么意思?
  • ¥15 STM32驱动继电器
  • ¥15 Windows server update services
  • ¥15 关于#c语言#的问题:我现在在做一个墨水屏设计,2.9英寸的小屏怎么换4.2英寸大屏
  • ¥15 模糊pid与pid仿真结果几乎一样
  • ¥15 java的GUI的运用
  • ¥15 我想付费需要AKM公司DSP开发资料及相关开发。