Dreamer
Dreamer

Reputation: 7551

How to submit form and retrieve data in JSON by jquery?

The normal form of jquery JSON request would through Ajax

j.ajax({
   type: 'POST',
   url: url,
   async: true,
   data: urlData,
   timeout: 20000,
   dataType: 'json',
   success: function(json) {

Problem to above solution is even post method still go through and URL. What I am trying to achieve, is that once click a button then the form data is submitted through a JSON call(so that it won't have to be a page submit) and the data return from the call is in JSON format.

So how to achieve the goal?

Upvotes: 0

Views: 229

Answers (2)

foly
foly

Reputation: 307

Add event.preventDefault(); to your submit event handler so that the default behavior will be prevented.

$("#yourForm").submit(function(event){
    event.preventDefault();
    //ajax call
});

Upvotes: 2

nima
nima

Reputation: 6733

You have to stop the default action of your submit button, if your using jquery:

$("#myButton").click(function(e) {
    e.preventDefault();
    // ...

If not, return false; after ajax call in your click event:

<input type="submit" onclick="doTheAjaxCall(); return false;"/>

Upvotes: 1

Related Questions