3

I want to get response of form's submit event in jquery or javascript ?

I am firing form's submit event like that in jquery :-

$(form).submit();

actually for checking the response i am doing it like that :-

alert('resp=' + $(form).submit());

And below is the action method :-

public ActionResult SaveProduct(ProductViewModel model)
   {           
       if (ModelState.IsValid)
       {
           return Json(true);
       }
       else
       {
           return Json(false);
       }

   }

So its just returning true or false. But i getting below response in alert(where i trigger it) :-

[object object]

And i want to know that,how i can get the submit event response like that ?

Pawan
  • 2,150
  • 11
  • 45
  • 73
  • 2
    If you're submitting the form, the page reloads, even if you trigger the form submit with jQuery, and all is lost, but you will get the form data on the serverside. – adeneo Oct 28 '13 at 13:52
  • You won't be able to do this easily with plain javascript. When you post a form, the form inputs are sent to the server and your page is refreshed. If you really wanted to get the response in Javascript (without the page refreshing), then you'll need to use AJAX. – Amit Jul 02 '14 at 11:02

4 Answers4

1
   $( "#target" ).submit(function( event ) {
        alert( "Handler for .submit() called." );
        event.preventDefault();
    });

For more info: http://api.jquery.com/submit/

Krish R
  • 22,583
  • 7
  • 50
  • 59
1

The call to .submit() itself, like many jQuery functions, is probably just returning a jQuery object. I'm surprised you're getting anything really. Isn't this call to .submit() causing the page to load the new content entirely? Or is this otherwise happening via AJAX that I'm just not seeing here?

Since the server-side code is just returning JSON data, I assume you don't want to load the page. Normally for an AJAX call I might otherwise just use the .post() function from jQuery. Something like this:

$.post('@Url.Action("SaveProduct", "YourControllerName")', $(form).serialize(), function (result) {
    // here the "result" variable will contain the response from the server
});

This is a bit of a mix of server-side code (using Razor syntax) and client-side code. The server-side call to Url.Action() is just to dynamically create the client-usable URL for that action method. The rest of this is just a call to the jQuery .post() function, passing it three arguments:

  1. The URL to call
  2. The data to send to the URL, which is from your form using jQuery's .serialize() function
  3. The function to handle the response from the server
David
  • 208,112
  • 36
  • 198
  • 279
  • David we can use this,but it does not cover one condition,if some how we don't have the model($(form).serialize()),then we can't use this approach. >> That's why i want to fire whether $('form').submit or $("#submitbuttonid").click() . >> Since by this the model will post automatically. – Pawan Oct 29 '13 at 09:48
  • @Pawan: It's not clear what you mean by this. You can attach the an event handler to the form's submit event or the button's click event (or both). Of course, you'll want to prevent the default action for those events (otherwise the form submit event will unload the whole page). All serializing the form does is turn its elements into key/value pairs for the POST event. I suppose you could do that manually for each element, but it generally makes more sense to just serialize the form. It has nothing to do with the MVC model, the model binder infers that from the posted values. – David Oct 29 '13 at 12:34
1

If you want to inspect responses you should use console.log vs alert, then check the response in the console.

   console.log($(form).submit())  
s1mpl3
  • 1,456
  • 1
  • 10
  • 14
0

For this you need to use jquery.form.js

Vinay
  • 49
  • 1
  • 3