[英]I'm using AJAX to post an object from javascript to php within the same page and I'm getting the entire html page instead of the object
[英]I'm using ajax call in ASP.NET application and I'm getting entire HTML page as response from jquery Ajax call
我正在開發一個ASP.NET Web應用程序。 在登錄屏幕上,我使用jQuery Ajax調用連接服務器端代碼。 在ajax調用的響應中,我獲取整個HTML頁面,而不是true或false(我希望從響應中獲得實際結果)。
下面是我的服務器代碼。
[WebMethod]
public static string LoginValidation(string userName, string passWord)
{
string connectionString = ConfigurationManager.ConnectionStrings["DBConnectionString"].ConnectionString;
SqlConnection con = new SqlConnection(connectionString);
SqlCommand cmd = new SqlCommand("select * from LoginUser where username = '"
+ userName + "' and password = '" + passWord + "'");
SqlDataReader rdr = cmd.ExecuteReader();
if (rdr.HasRows)
{
return "succes";
}
return "failure";
}
以下是我的Jquery Ajax調用。
$(document).ready(function () {
$('#txtLogin').focus();
});
$('#btnLogin').click(function () {
var username = $('#txtLogin').val();
var password = $('#txtPassword').val();
if (username === "" || password === "") {
alert('Both the fields are mandatory. Kindly try again');
$('#txtLogin').focus();
return;
}
else {
//ajax call to validate the login
$.ajax({
type: 'POST',
data: { 'userName': username, 'passWord': password },
url: 'Default.aspx/LoginValidation',
async: true,
datatype: 'text',
success: function (data) {
alert('test sadfadfa '+data+' '+data.responseText);
if (data == "failure") {
//redirecting to master page once after the successfull login
window.location.href = "/masterscreen.aspx";
}
else {
alert('false');
}
return;
},
error: function (data) {
}
});
//ajax call end
}
});
我在我的Ajax調用中包含了contentType。 它解決了我的問題。 現在,我得到了預期的JSON響應。 希望這會有所幫助。感謝那些幫助我的人。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.