$ .ajax总是会出错,为什么?

时间:2017-04-12 15:44:37

标签: javascript ajax jquery-mobile

我的代码是这样的:

    var WebServiceURL = "CityFixWS.asmx"; //the same as above. only with…

$(document).on("pagecreate", "#LoginPage", function () {
wireEventsLoginPage();
});

function wireEventsLoginPage() {
$('#LPBtnCheckUserByIDPass').click(function () {
    debugger;
    var id = $('#LPUserIDTXT').val();
    var pass = $('#LPUserPassTXT').val();

    var user = {
        idPar: id,
        passPar: pass
    }

    $.ajax({
        async: false,
        url: WebServiceURL + "/GetUserByNamePass",
        dataType: "json",
        method: "get", 
        data: JSON.stringify(user),
        contentType: "application/json; charset=utf-8",
        success: function (data) {
            var res = data.d;
            var resOutput = JSON.parse(res);
            alert("res=" + res);
            alert("resOutput= " + resOutput);
            if (resOutput != 'ERROR USER!') {
                alert(resOutput.Address + ", " + resOutput.Name);
                var jqHomePage = $("#HomePage");
                $.mobile.pageContainer.pagecontainer("change", jqHomePage, {});
            }
        },
        error: function () {
            alert("ERRRRRRRRRROR");
        },
    });
    alert('END');
});

我和调试器一起看到,当它出现在$ .ajax行时,下一步总是错误.... 嗯...我想要做的是在登录前检查用户的详细信息

这是请求网络方法

[WebMethod]
public string GetUserByNamePass(int userID, string passPar)
{
    User user = null;
    JavaScriptSerializer json = new JavaScriptSerializer();

    command.CommandText =
        " SELECT * " +
        " FROM Users " +
        " WHERE User_ID = @user_ID AND Password = @pass";

    command.Parameters.Add(new SqlParameter("@user_ID", userID));
    command.Parameters.Add(new SqlParameter("@pass", passPar));
    command.Connection.Open();
    SqlDataReader reader = command.ExecuteReader();

    if (reader.Read())
    {
        user = new User()
        {
            Id = (int)reader["User_ID"],
            Full_Name = reader["Full_Name"].ToString(),
            Birthday = (DateTime)reader["Birthday"],
            Password = reader["Password"].ToString(),
            ReportsCount = (int)reader["ReportCounts"]
        };
        command.Connection.Close();
        Console.WriteLine(user.ToString());
        return json.Serialize(user);
    }
    else
    {
        command.Connection.Close();
        return json.Serialize("ERROR USER!");
    }
}

}

<head>
<title></title>
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="stylesheet" href="http://code.jquery.com/mobile/1.4.5/jquery.mobile-1.4.5.min.css" />
<script src="http://code.jquery.com/jquery-1.11.1.min.js"></script>
<script src="http://code.jquery.com/mobile/1.4.5/jquery.mobile-1.4.5.min.js"></script>
<script src="scripts/jquery-migrate-3.0.0.min.js"></script>
<link href="StyleSheet.css" rel="stylesheet" />
<script src="CityFixJS.js">
</script>

这是我需要点击它的按钮

'<input type="button" id="LPBtnCheckUserByIDPass" value="התחבר" />'

1 个答案:

答案 0 :(得分:-1)

我认为它是一个解析错误,请尝试删除dataType:'json',看看它是否有效。