twice Form submission using JQuery

98 views Asked by At

I am novice in the UI, we are trying to implement a web call using a JQuery where we are have observer a strange issue of webcall i.e, when we are trying to do a post http call we have observer in server access logs first http post call is made and then immediately one http get call exist.

Please find below the .jsp page:

<html>

<head>
<script type="text/javascript"
    src="http://ajax.googleapis.com/ajax/libs/jquery/1.4/jquery.min.js"></script>
<%
    String urlVal = (String) request.getAttribute("dataURL");
%>
<%
    String emailID = (String) request.getAttribute("emailID") ;
%>

<script type="text/javascript">
var url="<%=urlVal%>";
alert(url);

     function navigateWithForm() {
        var form = $('<form></form>');
        form.attr({
            method: 'POST',

            action: url
        });
        form.append(getInput());
        form.appendTo('body').submit();

     }

     function getInput() {
         alert("<%=emailID%>");
        return $('<input type="hidden" />').attr({
            name: 'header',
            value: '<%=emailID%>'   
        });
    }

    $(document).ready(function() {
        navigateWithForm();
    });

</script>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>JSP Page</title>

</head>


</html>

In the backend the service are deployed in weblogic and it is designed in the spring 2.5 framework. We have apache proxy server for load balancing.

Any help will be appreciated.

2

There are 2 answers

0
ramesh027 On BEST ANSWER

Finally after many days struggle, i have found out the solution of the issue. which is really a small change. Hope the solution will be useful for the someone else who are struggling with the issue. Actual issue is with the url/action of the Jquery call.

URL passed earlier

Context:root?param1=value&param2=2

Correct URL passed is

Context:root/?param1=value&param2=2.

Thanks to Phil where i have to see the header of the request and response and other such as location, origin and referrer

5
Phil On

There is nothing particularly wrong with the code in your question (unless you actually wanted to use AJAX but I feel that's a different problem).

Your server-side code seems to be implementing a typical Post/Redirect/Get request handling pattern.

Here's what appears to be happening

Browser               |  Server
----------------------|-----------------------------
create form           |
  action=urlVal       |
  method=POST         |
                      |
submit form           |
  POST "urlVal" -------->
                      |
                      |  Process request
                      |
                    <--- Response
                      |    302 Found
                      |    Location: someUrl
                      |
GET "someUrl" ---------->
                      |
                    <--- Response
                      |    500 Internal Server Error

So the URL your server-side code is adding to the 302 Found response's Location header, is not capable of handling the GET request resulting from that response. You'll need to modify your server-side code to either handle the GET request or respond with a different Location that can.


For example (Spring MVC), say your urlVal is "/foo" and you have

@RequestMapping("/foo")
public String handleFoo(@RequestParam String header) {
    // process, etc
    return "redirect:/foo";
}

Returning a redirect response like that tells the client (browser) to perform a GET request via the response headers...

302 Found
Location: /foo

Notice how the method requires the header request parameter.

The browser will attempt to request /foo via GET which will fail because there is no request body with parameters (ie, no header).

The solution is to either:

  • Redirect to an existing URL that responds to GET requests
  • Handle GET requests for the URL you are redirecting to

    @RequestMapping(path = "/foo", method = RequestMethod.GET)
    public String getFoo() { // note, no required parameters
        // etc
    }
    

    or,

  • Respond in a different way. Even an empty response is fine

    @RequestMapping("/foo")
    @ResponseBody
    public ResponseEntity<Void> handleFoo(@RequestParam String header) {
        // process, etc
        return ResponseEntity.ok().build();
    }