views:

33

answers:

2

I have a modal dialog (done through jquery UI) that submit a form to a remote controller Action.

This is the jquery function called:

$("fpForm").submit(function() {
    $.ajax({
        type: "POST",
        url: "ForgotPassword",
        data: $("#fpForm").serialize(),
        success: function(response) {
            alert(response);
        },
        error: function(response) {
            alert(response);
        }
    });
});

The action does some verification on the data and then send back a response in JSON format. Let's say, for example, that this is a sample response:

{"result":"NOK","message":"The user is not registered on the system"}

My questions are:

  1. Why the debug alert that I have set in the "success" and "error" block does not are get executed?
  2. How I can write my code to parse the response while remaining in wait for it on the dialog?
  3. How can I write code to block the form elements during the ajax call?

I am sorry if the question could seem stupid for most of you but I am completely new to ajax and I am trying to learn throgh some experienced pattern that I know.

Thank you for your responses

+1  A: 

My questions are:

  1. Why the debug alert that I have set in the "success" and "error" block does not are get executed?
  2. How I can write my code to parse the response while remaining in wait for it on the dialog?
  3. How can I write code to block the form elements during the ajax call?
  1. If you meant to use the id then you missed the # designator:

    $("#fpForm")

  2. Add the sync : true option on the call?

  3. You could either: set the disabled attribute on the form elements AFTER posting the request, or else mask the page with an element (possibly semi-transparent) to divert the inputs.

Carter Galle
Thank you very much!I have included your suggestions but the first error is still there. i have also added the option "sync: true" to the call but I get redirected to another screen that contains only the json response!!!
Lorenzo
+1  A: 

The first error is the usage of $("fpForm").submit instead of $("#fpForm").submit.

If the server sand back JSON data, for example as JsonResult, you should include dataType: "json" to convert result to the object in object. After that you can replace alert(response); to

alert('Result: ' + response.result + ', Message: ' + response.message);

To block the form element I'll recommend you to use jQuery BlockUI Plugin. On the demos you will find different examples of usage and find the way which you like.

Oleg
Well... you're right on the first error but the form was submitted as well! Thanks anyway for your answer!!!
Lorenzo
I have included your suggestions but the first error is still there. i have also added the option "sync: true" to the call but I get redirected to another screen that contains only the json response!!!
Lorenzo
I think you don't need use `sync: true` if it is required. You program should works also without `sync: true`. It it is do not yet work the error is not fixed. What do you mean with the "first error"? Will be data send to the server? Will be the data send with the code or work standard form "submit"? Could you insert `alert("before $.ajax");` before the `$.ajax` call? You can use Fiddler (see http://www.fiddler2.com/fiddler2/) or Firebug to verify that data are really send and received.
Oleg
Ok! I explain it better... When I click the submit button the call is done to the server and I can even debug server-side the action called. So it seems that regarding the communication everything is ok. When I say first error I refer to the point numer 1 of my original question: "Why the debug alert that I have set in the "success" and "error" block does not are get executed?".
Lorenzo
@Lorenzo - try using jQuery's `preventDefault` method (http://api.jquery.com/event.preventDefault/). Add an `event` argument to the `submit` handler (jQuery automatically passes in an event object to handlers), then call the preventDefault method like so: `$("#fpForm").submit(function(event) { event.preventDefault(); /* make the ajax call */ });`
Bryan
@Bryan - Great advice!!! Now everything works!!!! Thank you very much!!!
Lorenzo
@Bryan: Good point Brayn! I want suggest the same. Another way will be rename the type of button from "submit" to "button" and `$("#fpForm").submit` to `$("#fpForm").click`. Then one be sure that no other submit process work.
Oleg
@Lorenzo: I found the error in your code which we all don't see at the beginning. The event handler function of `submit` event should **return false** (see for example remarks of http://msdn.microsoft.com/en-us/library/ms536972.aspx). It you do this, then you can remove `event.preventDefault();`.
Oleg
@Oleg: Yes. That would be the alternate way. Thanks!
Lorenzo