我正在使用ajax get。控制器中的响应正确,但是在ajax中返回404错误

时间:2018-07-02 06:57:54

标签: javascript c# ajax asp.net-mvc

java脚本是:

function LogOn() {

var sendData = "userName=" + UserName.value + "&password=" + Password.value + "&isRemember=" + isRemember.checked;

$.get('../Account/LogIn', sendData, function (Data, status, jqXHR) {

    if (Data.IsSuccess == 1) {
       alert('ok');
    }
}, 'Json').fail(function () {
    MessageError.removeAttribute('hidden');
    MessageError.innerHTML = "خطای سمت سرور ";
})
}
控制器httpclient中的

返回正确的结果。但是当javascript ajax返回resoinse时,返回404错误。 控制者帐户是:

public ActionResult LogIn()
{
    int isSuccess = 1;
    object _Result = null;

    try 
    { 
        HttpClientHandler handler = new HttpClientHandler()
        {
            AutomaticDecompression = DecompressionMethods.GZip | DecompressionMethods.Deflate
        };

        var client = new HttpClient(handler);
        client.DefaultRequestHeaders.Accept.Clear();
        client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));

        string username = Request["userName"].ToString();
        string password = Request["password"].ToString();
        string encryptPass = JJorianClassLibrery.DLL.Encryptions.Encrypt(password, Entity.EncryptKey);

        bool isRemember = bool.Parse(Request["isRemember"]);

        var response = client.GetAsync(GetBaseUrl() + "api/v1/User/Login/" + username + "/" + encryptPass).Result;

        if (response.IsSuccessStatusCode)
        {
            isSuccess = 1;
            var result = response.Content.ReadAsStringAsync().Result;
            _Result = result;
        }
        else
        {
            _Result = "نام کاربری یا کلمه عبور نادرست است";
            isSuccess = 0;
        }
    }
    catch
    {
        isSuccess = 0;
        _Result = "عملیات ورود با موفقیت انجام نشد";
    }

    return Json(new { IsSuccess = isSuccess, Message = _Result }, JsonRequestBehavior.AllowGet);
}

在控制器响应中正确并返回结果的json,但是当将结果传递给javascript时,获取ajax ret响应是404(未找到)...

2 个答案:

答案 0 :(得分:1)

我认为这很简单,您到控制器的链接是错误的...

尝试:

$.get('/Account/LogIn', sendData, function (Data, status, jqXHR)

答案 1 :(得分:0)

404错误表示ajax调用找不到控制器。

$.get('../Account/LogIn', sendData, function (Data, status, jqXHR)

您确定('../Account/LogIn')有效吗?也许您将需要文件扩展名(.php,.js等)或整个文件路径,例如('http://yourdomain.com/Account/LogIn.php')

希望获得帮助