views:

48

answers:

2

Hi,

I am developing application in Asp.net MVC.

I submit the form with using jquery:

var Data = $("#frmForgotPassword").serialize();
$.post("<%= Url.Action("ForgotPassword","Account") %>/", Data, function(retdata, textStatus) {
    if (textStatus == "success") {
        if (retdata.status == false) {
            $("#error").html('<p class="error">Error: ' + retdata.msg + '</p>');
        }
        else {
            $("#error").html('<div class="clean-ok">Success: ' + retdata.msg + '</div>');
        }
    }
    else
        alert("error: " + textStatus);
}, "json");

But I get the response as file open, shown here.

My controller returns the json as follow:

return Json(new { status = false, msg = "User name or email is not registered with us!" });

or

return Json(new { status = true, msg = "Your username and password has been sent to your email address!" });

So where is the mistake? How to stop opening response as file? It gives the same error in IE also.

EDIT:

Request header:

Host    localhost:16293
User-Agent  Mozilla/5.0 (Windows; U; Windows NT 6.1; en-US; rv:1.9.2.6) Gecko/20100625 Firefox/3.6.6
Accept  application/json, text/javascript, */*
Accept-Language en-us,en;q=0.5
Accept-Encoding gzip,deflate
Accept-Charset  ISO-8859-1,utf-8;q=0.7,*;q=0.7
Keep-Alive  115
Connection  keep-alive
Content-Type    application/x-www-form-urlencoded; charset=UTF-8
X-Requested-With    XMLHttpRequest
Referer http://localhost:16293/Account.aspx/LogOn?ReturnUrl=%2fdefault.aspx
Content-Length  15
A: 

I suspect this is being invoked as a handler on a link and that you have the link href set to the same url as the AJAX post. If that's the case, then I think you have a javascript error that is causing the link action (a GET) to be invoked instead of the AJAX post call. Check with Firefox/Firebug and see if there is an error in the console.

Also, you don't need a slash at the end of your URL in the post method. The query parameters will be appropriately appended to the URL without it.

tvanfosson
I get the javascript error for retdata, but when I open the file, the text response is correct!
Vikas
@Vikas - is the javascript error on page load? If so, your javascript code isn't running and the response is from clicking on the link. Fix your javascript error and see if it doesn't start working. Also, you could test this by requiring the request be a post using the HttpPostAttribute on the action. If the action requires a post and it's coming from a GET you'll get an exception that the action wasn't found.
tvanfosson
Vikas
May be I'm using jQuery Open dialog function to open the "Forgot password" page, response may lost it's origin!
Vikas
@Vikas - is it being opened from a link with the same href as the form? Are you returning false from the link's click handler? If you don't prevent the default action of the link, it's doing both the POST and a subsequent GET. In this case, you'd be seeing the result of the get as the file download. Can you post the bit of code surrounding what you've shown so that we can see how it gets invoked?
tvanfosson
A: 
return Json(new {...},JsonRequestBehavior.AllowGet);

How about this?

takepara