Note that there are some explanatory texts on larger screens.

plurals
  1. PO$.ajax function not working when <form> is used
    primarykey
    data
    text
    <p>I am learning ajax. Please find below code,</p> <pre><code> &lt;%@ page language="java" contentType="text/html; charset=ISO-8859-1" pageEncoding="ISO-8859-1"%&gt; &lt;!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org /TR/html4/loose.dtd"&gt; &lt;html&gt; &lt;head&gt; &lt;meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1"&gt; &lt;title&gt;Insert title here&lt;/title&gt; &lt;script type="text/javascript" src="/MyApp/js/jquery.js"&gt;&lt;/script&gt; &lt;script type="text/javascript"&gt; function doAjax() { alert('yes'); $.ajax({ url: 'register.html', data: ({name : "me"}), success: function(data) { $('#time').html(data); } }); } &lt;/script&gt; &lt;/head&gt; &lt;body&gt; &lt;form action=""&gt; &lt;button id="demo" onclick="doAjax()" title="Button"&gt;Get the time!&lt;/button&gt; &lt;div id="time"&gt; &lt;/div&gt; &lt;/form&gt; &lt;/body&gt; &lt;/html&gt; </code></pre> <p>This code makes successful ajax call to my spring controller and return data only if i remove form tags otherwise it wont show ajax message (and no error as well).</p> <p>I checked logs. If i remove form tag, last request received is to 'register.html' which is very correct. If i put form tag, on click of button, request goes to 'hello.html' which is the page which holds above code. I am not able to understand how is this happening.</p> <p>One thing I was suspecting is form is getting sumitted. but how can this happen if i am not firing sumit event on click of button.</p> <p>I need to have form tag on this page so please advice.</p> <p>controller functions below,</p> <pre><code> @RequestMapping("/hello") public ModelAndView helloWorld() { return new ModelAndView("hello", "message", "Spring MVC Demo"); } @RequestMapping(value = "/register", method = RequestMethod.GET) public @ResponseBody String registerUser(@RequestParam String name) { String result = "Time for registration" + name + " is " + new Date().toString(); return result; } </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload