简体   繁体   English

将JQuery ajax GET转换为POST

[英]Convert JQuery ajax GET to POST

I have this jquery ajax get method on an Index razor in my application: 我在我的应用程序中的索引剃刀上有这个jquery ajax get方法:

$.ajax({
    url: "@Url.Action("SubmitProjectForPreapproval", "api/Project")",
    type: "GET",
    cache: false,
    data: { projectId: "@ViewContext.RouteData.Values["ProjectId"]" }
}).done(function (data) {

    var count = 0;
    $.each(data, function (index, value) {
        $("#ulMessages").append("<li>" + value + "</li>");
        count++;
    });

    // Assume validation errors if more than 1 message
    if (count > 1) {
        $("#btnSubmit").removeAttr("disabled");
    }

}).fail(function () {

    $("#ulMessages").append("<li>An error occurred. Please try again later</li>");
    $("#btnSubmit").removeAttr("disabled");

}).always(function () {

    $("#imgAjaxLoader").hide();

});

This calls a method within the api/project controller that returns a list of strings: 这会在api / project控制器中调用一个返回字符串列表的方法:

[HttpGet]
public List<string> SubmitProjectForPreapproval(int projectId)
{ ... }

What I want to do is convert this to an ajax post method. 我想要做的是将其转换为ajax post方法。 I've been struggling to do just that for the better part of the day. 在一天中的大部分时间里,我一直在努力做到这一点。 My question is, what is everything that needs to change in order for this to happen? 我的问题是,为了实现这一目标,需要改变的是什么? eg - change attribute of the method to [HttpPost] , and how do I send it the route value? 例如 - 将方法的属性更改为[HttpPost] ,如何将路由值发送给它? (int pojectId) (int pojectId)

Edit: If I do this it works for some reason: 编辑:如果我这样做,它的工作原因是:

public List<string> SubmitProjectForPreapproval(/*int projectId*/)
{
    int projectId = 3308;
    ...
}

Not sure why it doesn't find my method if I have the parameter there. 如果我在那里有参数,不知道为什么它找不到我的方法。

I'm not sure how the @Url stuff formats with your system - but just changing it to something like: 我不确定@Url的东西是如何格式化你的系统的 - 但只是将其更改为:

$.ajax({ url: "@Url.Action("SubmitProjectForPreapproval", "api/Project")", type: "POST", cache: false, data: { projectId: "@ViewContext.RouteData.Values["ProjectId"]" } }).done(function (data) {

If you're bit: 如果你有点:

@Url.Action("SubmitProjectForPreapproval

..actually has any ?xxx values in it, you also need to add them into the data: { ... } ..实际上有任何?xxx值,你还需要将它们添加到data: { ... }

The problem was indeed the way I was sending the data to the controller action. 问题确实是我将数据发送到控制器操作的方式。 I fixed it by doing the following: 我通过执行以下操作修复了它:

  • Alter the ajax method (make use of JSON.stringify): 更改ajax方法(使用JSON.stringify):

var projectIdObject = {
    ProjectId: "@ViewContext.RouteData.Values["ProjectId"]",
}

$.ajax({
    url: "@Url.Action("SubmitProjectForPreapproval", "api/Project")",
        type: "POST",
        cache: false,
        data: JSON.stringify(projectIdObject),
        contentType: 'application/json',
    }).done(function (data) { ... }
  • Add a class to take this stringified value: 添加一个类以获取此字符串化值:

public class ProjectIdObject
{
    public string ProjectId { get; set; }
}
  • Alter the controller action method to receive this new object and extract the value I want: 更改控制器操作方法以接收此新对象并提取我想要的值:

[HttpPost]
public List<string> SubmitProjectForPreapproval(ProjectIdObject projectIdObject)
{
    int projectId = int.Parse(projectIdObject.ProjectId);
    ...
}

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

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