How to get response to form submit event in javascript / jquery?

I want a response to a form submit event in jquery or javascript?

I fire a form submit event like this in jquery: -

$(form).submit();

in fact, to verify the answer, I do it like this: -

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);
       }

   }

That way it just returns true or false. But I get below response in warning (where I run it): -

[object object]

And I want to know this, how can I get the sending response of the dispatch this way?

+4
source share
4 answers
   $( "#target" ).submit(function( event ) {
        alert( "Handler for .submit() called." );
        event.preventDefault();
    });

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

+1
source

.submit(), jQuery, , jQuery. , - . .submit(), ? AJAX, ?

JSON, , . AJAX .post() jQuery. - :

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

( Razor) . Url.Action() URL-, . jQuery .post(), :

  • URL-
  • URL-, jQuery .serialize()
+1

If you want to check the answers, you should use console.log vs alert, and then check the answer in the console.

   console.log($(form).submit())  
+1
source

For this you need to use jquery.form.js

0
source

All Articles