我调用Ajax从我的MVC项目中获取Web API的结果。在我的本地页面工作,但在生产中它不工作,并在.js文件中给我这个错误恰好在这一行:$ .ajax
http://mylink.cloudapp.azure.com/searchuser 404(未找到)
x.extend.ajax匿名函数
这是我的.js文件:
$(document).ready(function () {
$('#btnSearch').click(function (evt) {
// debugger;
if (ValidateInput()) {
var data = {
LastName: $.trim($('#LastName').val() || ''),
Zip: $.trim($('#Zip').val() || ''),
Ssn: $.trim($('#Ssn').val() || '')
};
var token = $('[name=__RequestVerificationToken]').val();
$.ajax({
dataType: "json",
//headers: { "__RequestVerificationToken": token },
data: data,
url: '/searchuser',
type: 'POST',
cache: false,
success: function (result) {
console.log(result);
if (result && result.success) {
$('#ApplicationId').val(result.data.applicantId);
if (result.data.exception == null) {
$('#stepTwo').show();
$('#EmailAddress').val(result.data.userEmailAddress);
}
else {
$('#txtareaResponse').val(result.data.exception);
}
}
},
error: function () { debugger; alert('failure'); }
});
}
});
这是我的观点之一:
<script src="~/Scripts/jquery-1.10.2.min.js"></script>
<link href="~/Content/Loan.css" rel="stylesheet" />
<script src="~/Scripts/Verify.js"></script>
这是控制器方法:
[AllowAnonymous]
[Route("searchuser")]
[HttpPost]
public async Task<ActionResult> SearchUser(UserInfo userInfo)
{
object userObject = null;
if (userInfo.LastName != null && userInfo.Zip != null && userInfo.Ssn != null)
{
string accessKey = CreateAccountKey(userInfo.LastName, userInfo.Zip, userInfo.Ssn);
UserKey userKey = new UserKey();
userKey.AccountKey = accessKey;
//var response = await httpClient.GetAsync(string.Format("{0}{1}/{2}", LoanApiBaseUrlValue, "/verifyuser", accessKey));
var response = await httpClient.PostAsJsonAsync(string.Format("{0}{1}", LoanApiBaseUrlValue, "/verifyuser"), userKey);
if (response.IsSuccessStatusCode)
{
userObject = new JavaScriptSerializer().DeserializeObject(response.Content.ReadAsStringAsync().Result) as object;
var json = response.Content.ReadAsStringAsync().Result;
var userVerify = new JavaScriptSerializer().Deserialize<VerifyUser>(json);
}
}
var respone = new
{
success = userObject != null,
data = userObject
};
return Json(respone, JsonRequestBehavior.AllowGet);
}
答案 0 :(得分:1)
尝试返回ActionResult
[AllowAnonymous]
[Route("searchuser")]
[HttpPost]
public ActionResult SearchUser(..){..}
也在你的ajax调用中使用Razor语法
$.ajax({
url: "@Url.Action("method", "Controller")",
type: "GET",
data: {},
success: function (data) {
//do stuff...
}
});