简体   繁体   English

Ajax调用返回未定义的数据

[英]Ajax call returns undefined data

I have an ajax call that requests data from an MVC controller method. 我有一个ajax调用,它从MVC控制器方法请求数据。
I am returning a Json result from the controller. 我从控制器返回Json结果。

Ajax request completes, but the data returned is undefined. Ajax请求完成,但是返回的数据不确定。

Ajax Call 阿贾克斯电话

var param = {
    "username": uname,
    "password": pass
};
var serviceURL = "/Account/CheckUser";

var req = $.ajax({
    url: serviceURL,
    type: "POST",

    data: JSON.stringify(param),

    contentType: "application/json",

    complete: successFunc,
    error: errorFunc
});

function successFunc(data) {
    if (data.exists == true) {
        console.log("Completed : " + data.exists);
    } else {
        console.log("Failed : " + data.exists);

    }
}


Controller Method 控制器方式

[HttpPost]
public JsonResult CheckUser(string uname, string pass)
{
    Boolean cont = true;
    return Json(new { exists = cont });
}



Can anyone tell me why exists returns as undefined? 谁能告诉我为什么exists返回值是不确定的?

UPDATE 更新
As suggested below, I wrote the data to the console, and it seems it's returning an empty string. 如下面的建议,我将数据写入了控制台,看来它正在返回一个空字符串。 So I guess the question should be more ' Why is the data returning empty? 所以我想问题应该更多( 为什么数据返回为空?

The function you specify via the complete option doesn't receive the data (for good reason: it's called even if there is no data, because there was an error). 您通过complete选项指定的函数不会接收数据 (有充分的理由:即使没有数据,也会调用它,因为有错误)。 Change complete: to success: . 更改complete: success:

var req = $.ajax({
    url: serviceURL,
    type: "POST",

    data: JSON.stringify(param),

    contentType: "application/json",

    success: successFunc, // <=== Here
    error: errorFunc
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM