0
我正在Xamarin表單PCL中重新創建一箇舊的Cordova應用程序,並且需要訪問服務器上的此方法,爲其提供用戶名和密碼並存儲返回的信息:JSON,使用HttpPost獲取信息
[HttpGet]
public JsonResult LoginUser(string userName, string password)
{
bool responseResult = false;
IEBUser user = null;
string errMsg = String.Empty;
try
{
if (String.IsNullOrEmpty(userName))
{
throw new Exception("userName is Empty");
}
else if (String.IsNullOrEmpty(password))
{
throw new Exception("userName is Password");
}
// connect to DB and find the user if it can
user = SelfServiceMembership.GetUserByUserName(userName);
// if no suer then user wasn't found or DB Errored
if (user == null)
{
throw new Exception("userName was not found");
}
// decrypt pw and see if they match with username's account
PasswordHash PH = new PasswordHash();
password = PH.GenerateHash(password);
if (user.HashWord != password)
{
throw new Exception("Password does not match the one on our records");
}
responseResult = true;
}
catch (Exception ex)
{
errMsg = ex.Message;
}
if (responseResult)
{
return Json(new
{
result = responseResult,
user = new
{
userId = user.UserID,
userName = user.UserName,
firstName = user.FirstName,
lastNmae = user.LastName,
email = user.Email
}
},
JsonRequestBehavior.AllowGet);
}
return Json(new
{
result = responseResult,
errorMessage = errMsg
},
JsonRequestBehavior.AllowGet);
}
舊的Javascript代碼調用此方法是這樣的:
// gets user info from web service
loginUser: function (userName, password){
responseData = null;
$.ajax({
type: "GET",
url : app.CurrentCompanyDetails.WebServiceURL + this.apiRoot + this.loginUserCall,
dataType: "json",
data: {
userName: userName,
password: password
},
async: false,
crossDomain: true,
success: function(response) {
responseData = response;
},
error: function (xhr, status, msg) {
alert(msg);
}
});
return responseData;
},
我似乎無法在任何地方找到一個明確的答案如何做到這一點在C#
你嘗試改變'HttpGet'爲'HttpPost'和'類型: 「GET」''爲類型: 「POST」'? –
@FabricioKoch對不起,我不知道你在說什麼。 JavaScript代碼需要用C#編寫。 – connersz
對不起。我認爲你正在嘗試修復你的javascript調用。 –