简体   繁体   English

AJAX中的成功或错误回叫不起作用

[英]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#代码的简单Ajax请求。我确定C#会给出正确的值(根据情况)。

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. C#会根据情况返回truefalse 。但是无论如何,此Ajax脚本都不会给我提供我已为其编码的警报窗口。

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. 如果为false ,则只有标签中的值更改为true。 Can anyone tell me why neither success or error is not working. 谁能告诉我为什么successerror都不起作用。

As you can tell from it's name the .serializeArray() method returns an array -- not an object. 从名称可以看出, .serializeArray()方法返回一个数组-不是对象。 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 . 因此,要访问第三个value您需要提供索引2- ..[2].value ... name ...[2].name Your code has errors that would prevent the ajax call from being made. 您的代码中有错误,这些错误会阻止进行ajax调用。 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");
    });
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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