0

How can I attach success event handler to a simple htmp post form that has such a markup:

<form action="/Home/UpdateMessage" method="POST">
...
</form>

On my server I have this response:

return Json(true, JsonRequestBehavior.AllowGet)

and it gets back to the client(browser) but how can I handle it?

mathinvalidnik
  • 1,566
  • 10
  • 35
  • 57

2 Answers2

1

If you are returning JSON then the post request has to be asynchronous or else the browser will just display the raw JSON.

Or you could return another route with the data you need in the headers and access them with Javascript, like so: Accessing the web page's HTTP Headers in JavaScript

Seeing as you've got the ajax tag I guess you're looking to post the form with ajax and get the response. See: Simple ajax form using javascript no jQuery

But I'd recommend using JQuery for it's ajax helpers: http://hayageek.com/jquery-ajax-form-submit/

Community
  • 1
  • 1
Mr Office
  • 290
  • 1
  • 9
1

Standard HTML form request don't have handlers or callbacks... When you submit a form request, the server receives it then emits an answer, but as there aren't any handlers the server should answer another web page.

If you want to receive data from the server you must do an AJAX call that has handlers to manage the server responses (succeed, error, etc.).

Eduardo Yáñez Parareda
  • 9,126
  • 4
  • 37
  • 50