Asp.NET MVC Ajax-Post FORM and Ajax-Get

I am stuck, who can help me? In my LogOn.aspx view, I have login controls (Username, Password, RememberMe) inside a FORM tag:

<form id="loginform" method="post" action="/Account/LogOn/">

      

Below, I have a hidden DIV with OPTION dropdown and confirm button using onclick_event:

$.ajaxSetup({ cache: false });
$.ajax({
    type: "GET",
    url: "/Account/SetCompanyAndContinue",
    data: "{ 'id' : '1'}",
    contentType: "application/json; charset=utf-8",
    dataType: "json"
});

      

First, the user logs in. In jQuery, I am sending login credentials via AJAX:

var loginCred = new Object();
loginCred.Username = $('#userName').val();
loginCred.Password = $('#password').val();
loginCred.RememberMe = $('#rememberMe').checked;

var myJsonObject = JSON.stringify(loginCred);

$.ajaxSetup({ cache: false });
$.ajax({
    type: "POST",
    contentType: "application/json; charset=utf-8",
    url: "/Account/LogOnAjax/",
    data: myJsonObject,
    dataType: "json",
    success: function(data) {
        PostCredentialsSuccess(data);
    }
});

      

This POST works fine. The breakpoint in the Controller action hits the debugger and returns a JSON data object. I was putting JSON data into OPTION dropdown. This list of options is then presented to the user. Then, when the user clicks on the submit button, a second AJAX call is made:

$.ajaxSetup({ cache: false });
$.ajax({
    type: "GET",
    url: "/Account/SetCompanyAndContinue",
    data: "{ 'id' : '1'}",
    contentType: "application/json; charset=utf-8",
    dataType: "json"
});

      

I would expect the Controller action named "SetCompanyAndContinue" to be removed:

    [AcceptVerbs(HttpVerbs.Get)]
    public ActionResult SetCompanyAndContinue(string id)
    {
        SessionAdapter.CustomerID = Convert.ToInt32(id);
        return null;
    }

      

But that doesn't happen, instead, the default Controller Action hits the first time:

    public ActionResult LogOn()
    {
        return View();
    }

      

BUT (!) The second time I click (same) the submit button, the Controller action finally gets to [SetCompanyAndContinue].

Can someone tell me what I am doing wrong? Thank you very much in advance.

+2


source to share


6 answers


Silly mistake on my behalf:



  • I added a cookie to the response stream which caused the webbrower to behave unpredictable.
  • I forgot to mark this as an answer.
0


source


you need to pass data property as javascript object not as string

    $.ajax({ type: "GET",
    url: "/Account/SetCompanyAndContinue",
    data: ({id : 1}),
    contentType: "application/json; charset=utf-8", 
    dataType: "json" });

      



I recommend using firebug to see the actual HTTP request that the message is receiving from jQuery, and so tiny errors like this become apparent very quickly.

HTH

+2


source


try using the submit link instead of the normal submit button, it probably has a conflict somewhere between the click, ajax and the form.

0


source


I didn't know if I received the correct information. But as you describe, any click on the submit button will try to execute the Publish method, not the Receive method. The Get call is made only the first time you visit the page or refresh the URL. Use the link button to perform the Get action.

This is why, in your example, calling it "/ Account / LogOnAjax /" is an action with a Publish method.

0


source


You can use an HTTP Debug Proxy like Fiddler or Charles to compare different GET requests .

0


source


marc.d was right

A general rule of thumb (at least this works for me): if your action doesn't hit as you expect, chances are there is something wrong with the route parameters, or if you hit the default route, the form data sent with the post are not expected. I used $ (this) .serialize () as the jQuery data argument for $ post and one of my hidden form fields was blank. An action that was not included in any route (in which case I allowed it to go up to the default) never saw the message.

0


source







All Articles