繁体   English   中英

Ajax调用成功现在正在工作 - 传递JSON对象[String]

[英]Ajax Call Success Now Working - Passing JSON Object [String]

我正在尝试在我的应用程序中开发一些验证,以检查数据库中是否存在函数名称 而不是使用SQL我想使用ajax传递这些数据。

不幸的是,当尝试传递这个JSON(String)对象时response永远不会success: function (response)传递success: function (response) ,我收到一条错误消息。

我希望有一些建议,为什么会发生这种情况,因为我相信我的代码是在正确的格式内才能成功完成。

我尝试将数据类型更改为text ,并且还包含contentType: 'application/json; charset=utf-8 contentType: 'application/json; charset=utf-8但这并没有帮助解决这个问题。

function AllowedFunction(FunctionName) {
    var result = "None";        
    $.ajax({
        url: '@Url.Action("FunAllowed")',
        type: "POST",            
        dataType: "JSON",
        data: { FunctionName: FunctionName },
        success: function (response) {
            if (response.length > 0)
            {
                result = "True";
            }
            else
            {
                result = "False";
            }              
            }            
    });
    return result;
}



    // VAL: Function Name Allowed
    public JsonResult FunAllowed(string FunctionName)
    {            
        var records = db.Functions.Where(x => x.FunctionName == FunctionName).ToList();
        string result = "False";

        if (records.Count > 0)
            result = "True";

        return Json(records, JsonRequestBehavior.AllowGet);
    }

ReferenceError:响应未在eval中定义(eval at AllowedFunction( http:// localhost:52613 / Functions / Create:50:9,: 1:1) 1。message :“response is not defined” 2。stack :“ ReferenceError:未定义响应

我想这可能是因为你的FunAllowed函数期望一个string ,但是你传递的是一个名为FunctionName属性object ,它是一个string

我想如果你这样做,它可能会工作而不改变你的JS代码:

class FunAllowedinput
{
    public string FunctionName{get;set;}
}
public JsonResult FunAllowed(FunAllowedInput input)
{            
    var records = db.Functions.Where(x => x.FunctionName == input.FunctionName).ToList();
    string result = "False";

    if (records.Count > 0)
        result = "True";

    return Json(records, JsonRequestBehavior.AllowGet);
}

暂无
暂无

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

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