简体   繁体   English

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

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

I am trying to develop some validation within my application to check if a Function Name all ready exists within the database. 我正在尝试在我的应用程序中开发一些验证,以检查数据库中是否存在函数名称 Rather than using SQL I wanted to use ajax to pass this data through. 而不是使用SQL我想使用ajax传递这些数据。

Unfortunately when trying to pass this JSON (String) Object , the response is never being passed success: function (response) and I am getting an error message. 不幸的是,当尝试传递这个JSON(String)对象时response永远不会success: function (response)传递success: function (response) ,我收到一条错误消息。

I was hoping for some advise as to why this might be occurring as I believe my code is within the correct format to complete successfully. 我希望有一些建议,为什么会发生这种情况,因为我相信我的代码是在正确的格式内才能成功完成。

I have tried changing the datatype to text and have also included contentType: 'application/json; charset=utf-8 我尝试将数据类型更改为text ,并且还包含contentType: 'application/json; charset=utf-8 contentType: 'application/json; charset=utf-8 but this has not helped solve the issue. 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: response is not defined at eval (eval at AllowedFunction ( http://localhost:52613/Functions/Create:50:9 ), :1:1) 1. message: "response is not defined" 2. stack: "ReferenceError: response is not defined ReferenceError:响应未在eval中定义(eval at AllowedFunction( http:// localhost:52613 / Functions / Create:50:9,: 1:1) 1。message :“response is not defined” 2。stack :“ ReferenceError:未定义响应

I think it's probably because your FunAllowed function is expecting a string , but you're passing an object with a property named FunctionName which is a string . 我想这可能是因为你的FunAllowed函数期望一个string ,但是你传递的是一个名为FunctionName属性object ,它是一个string

I think if you did this it might work without changing your JS code: 我想如果你这样做,它可能会工作而不改变你的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