【问题标题】:How could i pass parameters to method in a controller through jquery ajax calls我如何通过 jquery ajax 调用将参数传递给控制器​​中的方法
【发布时间】:2012-06-20 00:21:35
【问题描述】:

我正在研究 asp.net web api。我正在尝试使用 jquery ajax 调用来调用控制器(安全)。我的控制器中有一个带有 3 个参数的方法,例如,

public WebRequest GetRequest(string method,string type,string endpoint)
        {
        RSACryptoServiceProvider rsa=new RSACryptoServiceProvider();
        var request = WebRequest.Create(endpoint);
        request.Method = method;
        request.ContentType = type;
        UnicodeEncoding enc = new UnicodeEncoding();
        ServicePointManager.ServerCertificateValidationCallback = delegate { return true; };
        request.Headers.Add("Authorization-Token", RSAClass.StringConverter(RSAClass.RSAEncrypt(enc.GetBytes("User1"), rsa.ExportParameters(false), false)));
        return request;
}

我正在做一个 jquery ajax 调用,例如,

CreateRequest("GET", "application/json;charset=utf-8", "http://localhost:49847/api/Security", function (request) { alert("Hello"); });  


function CreateRequest(method, type, endpoint, callback) {
        $.ajax({
            url: "api/Security",
            data: { method: method, type: type, endpoint: endpoint }, 
            type: "GET",
            contentType: "application/json;charset=utf-8",
            statusCode: {
                200: function (request) {
                    callback(request);
                }
            }
        });

而且我还有 customfilterattribute 类来验证授权令牌,例如,

public override void OnActionExecuting(System.Web.Http.Controllers.HttpActionContext actionContext)
        {
            string token;
            try
            {
                token = actionContext.Request.Headers.GetValues("Authorization-Token").First();
            }
            catch
            {
                actionContext.Response = new HttpResponseMessage(System.Net.HttpStatusCode.BadRequest) { Content = new StringContent("missing authorization token") };
                return;
            }
            try
            {
                RSACryptoServiceProvider RSA = new RSACryptoServiceProvider();
                UnicodeEncoding enc = new UnicodeEncoding();
                AuthUsersRepository.GetAllUsers().First(x => x.Name ==enc.GetString(RSAClass.RSADecrypt(RSAClass.ByteConverter(token), RSA.ExportParameters(true), false)));
                base.OnActionExecuting(actionContext);
            }
            catch
            {
                actionContext.Response = new HttpResponseMessage(System.Net.HttpStatusCode.Forbidden) { Content = new StringContent("Unauthorized User") };
                return;
            }
        }

当我提出第一次请求时,它要求我提供授权令牌。并且还在 3 个参数(方法、类型、端点)中显示空值。指导我。

【问题讨论】:

    标签: asp.net jquery asp.net-web-api


    【解决方案1】:

    你需要这个:

    data: { 'method': method, 'type': type, 'endpoint': endpoint },
    

    这会传递请求中的值,尽管在您的示例中我不确定如果端点的值是控制器/操作的 URL,为什么需要将端点作为参数传递给方法?

    【讨论】:

    • @Aliostad 您可以通过 GET 或 POST 传递数据。文档声明数据将被添加到 GET 的查询字符串中。 api.jquery.com/jQuery.ajax
    • 谢谢。您能否更新您的帖子,以便我可以将我的 -1 更改为 +1?
    • 任何更新都可以。 SO 不允许我在几分钟后更改 mu upvote downvote。
    • @Aliostad 我已经尝试过了!
    【解决方案2】:

    你要么需要

    1) 更改您的 GET 以将数据发布为 JSON。 GET 不能有内容。 2) 将数据作为查询字符串参数传递

    如果我自己做,我会选择案例 2:

    http://localhost:49847/api/Security?method=someMethod&type=someType&endpoint=someendpoint 
    

    由于这与安全相关,必须是 HTTPS

    但我不会自己做,而是使用安全专家开发的:“Thinktecture.IdentityModel.40”

    https://github.com/thinktecture/Thinktecture.IdentityModel.40

    【讨论】:

    猜你喜欢
    • 2021-03-10
    • 2017-04-18
    • 1970-01-01
    • 2021-09-05
    • 1970-01-01
    • 2020-02-26
    • 1970-01-01
    • 2011-07-27
    • 1970-01-01
    相关资源
    最近更新 更多