【问题标题】:How can I make the following button call the GET method and not the POST method?如何使以下按钮调用 GET 方法而不是 POST 方法?
【发布时间】:2018-12-19 23:47:37
【问题描述】:

我有以下按钮:

<a id="commons" href="#" onclick="$('#sideViewAside').load('@Url.Action("PurchaseOrderSideView")', {orderId: '@item.OrderId'});" data-ma-action="sidebar-open" data-ma-target="#detailsTab" class="hidden-xs ma-trigger btn btn-icon command-edit waves-effect waves-circle"><span class="zmdi zmdi-view-list"></span></a> 

我的Controller 同时具有GETPOST 方法。当我运行代码时,单击此按钮时,将调用 POST 方法,而我希望调用 GET 方法。我在这里做错了什么?

【问题讨论】:

  • 您正在包含数据。这会触发 POST。如果你想要一个 GET,你需要使用查询字符串参数。
  • 哦。但我需要将orderId 传递给我的GET 方法,因为它是我的GET 方法的输入参数。如何添加?
  • 如果你想要一个GET,你需要使用查询字符串参数。我相信Url.Action() 的重载允许添加参数,如以下问题所示:Url.Action parameters?

标签: asp.net-mvc razor asp.net-core-mvc


【解决方案1】:

对于这个带有Post的代码发送请求,是因为你将对象传递给.load函数造成的。对于.load,其默认方法为Get,但如果与对象一起使用,它将使用Post

如果数据作为对象提供,则使用 POST 方法;否则,假定为 GET。

您可以尝试下面的代码以使用GetorderId 作为输入。

<a id="commons" href="#" 
  onclick="$('#sideViewAside')
  .load('@Url.Action("PurchaseOrderSideView",
  new {orderId = "111"})')" data-ma-action="sidebar-open" data-matarget="#detailsTab"
class="hidden-xs ma-trigger btn btn-icon command-edit waves-effect waves-circle">
<span class="zmdi zmdi-view-list">Test1</span>

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-09-19
    • 2018-01-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多