使用JS或jQuery处理表单提交中的500个错误? [英] Handle 500 error on form submit with JS or jQuery?

查看:680
本文介绍了使用JS或jQuery处理表单提交中的500个错误?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用标准的表单/操作发布到宁静的Web服务,由于表单的大小和组成,我试图不使用Ajax.有没有办法将错误处理附加到表单提交?请参阅下面的当前代码.

I'm using a standard form/action to post to a restful web service, I'm trying not to use ajax due to the size and makeup of the form. Is there a way to attach error handling to the form submission? See my current code below.

<form   id="theForm" 
        action="rest/url" 
        method="post" 
        enctype="multipart/form-data">
</form>

$("#theForm").submit(function(e){           
    // Can anything be done to handle 500s here?            
});

这是休息服务.

@POST
@Path("/save")
@Consumes(MediaType.MULTIPART_FORM_DATA)
public void save(   
    @FormDataParam("iD") Integer iD,
    ....
    @FormDataParam("file") InputStream inputStream,
    @FormDataParam("file") FormDataContentDisposition fileDetail
    {
        // void to avoid redirect

        // exceptions return 500
        throw new ServerException(500);
    }

推荐答案

尝试一下:

<form method="post" id="form>

</form>

Ajax代码:

$('#form').submit(function () {
  $.ajax({
    url: 'your_url.html',
    data: 'some_data=somevalue',
    success: function (response) {
      // it was a success!
    }
    error: function () {
      // it was an error!
    }
  });
});

请注意,您无法处理正常的提交过程! HTTP响应只能检查ajax请求.简单的表单提交无法处理!

Please note you cannot handle the normal submission process! Only ajax requests can be checked by HTTP response. Simple form submit cannot be handled!

您可以通过单击提交按钮来执行以上代码!

You can execute the above code on a submit button click!

如果您不想使用ajax,则需要使用服务器端代码.并检查服务器的HTTP Resonse!如果是500,则将用户重定向到另一个页面!但是请记住,您不能使用jQuery来检查HttpResponseCode的默认提交过程!

If you don't want to use ajax, then you need to use a server-side code. And check for the HTTP Resonse from the server! If it is 500, then redirect the user to another page! But remember, you cannot use jQuery to check the HttpResponseCode for the default submission process!

开始学习web.config更好.在该文件中,您可以管理错误以及服务器对错误和其他情况的响应方式的其他自定义.

It is better if you start learning about web.config. That is the file where you manage the error and other customization of how server responds to the error and other conditions.

这篇关于使用JS或jQuery处理表单提交中的500个错误?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆