简体   繁体   English

jQuery发布到servlet和刷新父页面

[英]jQuery post to servlet and refreshing parent page

This is what I am trying to do, based on the ajax jQuery documents I am reading. 这是我正在尝试做的,基于我正在阅读的ajax jQuery文档。 I want to send a post call to a servlet I wrote, which will save an entry in my database. 我想向我编写的Servlet发送一个调用,它将在数据库中保存一个条目。 After that post is successful, I want to reload the PARENT of the iFrame that renders my initial page. 发布成功后,我想重新加载呈现初始页面的iFrame的父项。

So what I currently have correctly makes the post call, but I have no parameters outside of the callback parameter. 因此,我目前正确地进行了后期调用,但是在回调参数之外没有任何参数。

So I have a couple questions: 所以我有几个问题:

  • Am I doing the post correctly? 我在正确地张贴帖子吗? Do I actually need to post the parameter list as a jsonp string? 我实际上是否需要将参数列表发布为jsonp字符串?
  • How would I correctly send a "succeed" call from the servlet AFTER I submit to the DB? 提交到数据库后,如何正确地从servlet发送“成功”调用?

Servlet: Servlet:

public class MyServlet extends HttpServlet {

    private static final long serialVersionUID = 1L;

    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException 
    {
        System.out.println(request.getQueryString());
    }
}

Result from my servlet: 我的servlet的结果:

callback=jQuery183010119481382259055_1374511172363

View call (jQuery): 查看通话(jQuery):

<script type="text/javascript" language="Javascript">
function funcCallBack() {
    var param12 = $('input:radio[name=param1]:checked').val();
    var param22 = $('[name="param2"]').val();
    var param32 = $('[name="param3"]').val();

    $.ajax({
        type: "POST",
        data: {param1:param12, param2:param22, param3:param32},
        url: 'http://localhost:8080/App/url',
        dataType: 'jsonp',
        beforeSend:function(){$('#replace').empty().append('&lt;div style="width:54px;height:55px;position:absolute;left:50%;top:50%;margin:-27.5px 0 0 -27px;"&gt;&lt;img src="/App/images/loading.gif" alt="Loading Results"/&gt;&lt;/div&gt;');}
    }).done(function() { alert('success'); });
}
</script>

Am I doing the post correctly? 我在正确地张贴帖子吗? Do I actually need to post the parameter list as a jsonp string? 我实际上是否需要将参数列表发布为jsonp字符串?

Yes, you are doing it correctly. 是的,您做得正确。 And you dont need to pass the parameter list as jsonp . 而且您不需要将参数列表作为jsonp传递。 Correct type for data is PlainObject or String . 数据的正确类型是PlainObjectString
datatype indicates the type of data that you're expecting back from the server and not the type of data you are sending. datatype表示您希望从服务器返回的数据类型,而不是要发送的数据类型。

How would I correctly send a "succeed" call from the servlet AFTER I submit to the DB? 提交到数据库后,如何正确地从servlet发送“成功”调用?

No, You dont need to explicitly indicate a successful return. 不,您不需要明确表示成功的回报。 success function is invoked when the request succeeds. 请求成功时将调用success函数。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM