簡體   English   中英

在ajax中獲取匿名函數錯誤

[英]getting anonymous function error in ajax

我調用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);
        }

嘗試僅返回ActionResult

[AllowAnonymous]
[Route("searchuser")]
[HttpPost]
public ActionResult SearchUser(..){..}

在您的ajax調用中也使用Razor語法

$.ajax({
    url: "@Url.Action("method", "Controller")",
    type: "GET",
    data: {},
    success: function (data) {
            //do stuff...
    }
});

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM