Question

I'm trying to catch data from the AJAX POST, which I've sent via jQuery to controller endpoint of ASP.NET MVC, like this:

$("form#auth").submit(function() {
    var login = $('input[id=login]').val();
    var password = $('input[id=password]').val();

    $.ajax({
        url: "/Home/Auth",
        type: "POST",
        data: "Login=" + login + "&Password=" + password,
        dataType: 'json',
        contentType: 'application/json; charset=utf-8'
        success: function() {

        }
    });

I've tested the controller understads what I'm sending to him, but the main problem is with the returning the result for my jQuery function.

I'm returning the result from Controller like this: http://ideone.com/hNkF3Z

But I don't understand why the server is returning a file for download:

If to open the file, the result is valid: {"Result":"failed"}

I know, that I didn't write a code in a success function in JavaScript, but I think server must not return a file download and also the debugger must stop at the breakpoint, which was defined on last scope } of success function.

Was it helpful?

Solution

What you're seeing is the default form submission. Because you don't prevent the default browser action, the form is still being posted as if the event handler wasn't even there.

Update your JavaScript to prevent the default browser behavior with e.preventDefault()

$("form#auth").submit(function(e) {
    e.preventDefault();
    /* rest of your code here. */
 }
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top