简体   繁体   中英

Success or error call back in AJAX not working

$(document).ready(function () {
$("#loginForm").submit(function (e)
{
    var Data = $(this).serializeArray();
    var formURL = $(this).attr("action");
    var PostData =
    {
        "CompanyName": $(this).serializeArray().CompanyName,
        "Username": $(this).serializeArray().Username,
        "Password": $(this).serializeArray().Password
    }
    $.ajax(
    {
        url: formURL,            
        data: PostData,
        success: function (data, textStatus, jqXHR) {
            alert("Data" + data);
            alert("Jq" + jqXHR);
            alert("textStatus" + textStatus);
        },
        error: function (jqXHR, textStatus, errorThrown) {
            alert("Failed..ajax error response type " + textStatus);
        }
    });
    e.preventDefault(); //STOP default action
})
});

$("#loginForm").submit(); //SUBMIT FORM

This is a simple Ajax request to the C# code,that i have.I know for sure that the C# is giving a correct value(according to the situation).

C# return true or false as per the situation.But in any case this Ajax script neither giving me a alert window which i have coded for.

Instead i get this response from

This XML file does not appear to have any style information associated with it. The document tree is shown below. <boolean xmlns="http://schemas.microsoft.com/2003/10/Serialization/">false</boolean>

When false and just the value in the tag changes when its true. Can anyone tell me why neither success or error is not working.

As you can tell from it's name the .serializeArray() method returns an array -- not an object. The array is of the form:

[
  {
    name: "a",
    value: "1"
  },
  {
    name: "b",
    value: "2"
  },
  {
    name: "c",
    value: "3"
  }
]

Therefore to access the third value you would need to supply the index 2 -- ..[2].value ... name ...[2].name . Your code has errors that would prevent the ajax call from being made. Is it possible that error is coming from somewhere else?

Therefore change:

var PostData =
{
    "CompanyName": $(this).serializeArray().CompanyName,
    "Username": $(this).serializeArray().Username,
    "Password": $(this).serializeArray().Password
}

To:

var PostDate = $(this).serialize();

Something that make work better is this:

$("#loginForm").submit(function (event) {
    event.preventDefault();
    $.post($(this).attr("action"), $(this).serialize())
        .done(function (results) {
        alert(results);
    })
        .fail(function (error) {
        alert(error);
    })
        .always(function () {
        alert("AJAX Complete");
    });
});

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM