【问题标题】:Can't pass querystring using $.ajax无法使用 $.ajax 传递查询字符串
【发布时间】:2025-12-22 06:05:13
【问题描述】:

使用下面的代码不会将查询字符串“section”添加到链接中。 url 添加得很好,但是我对查询字符串做错了什么?

@Html.Hidden("Url", Request.RawUrl)
@Html.Hidden("Query", sectionGroup.Term)

<a href="#" id="ajaxLink">@sectionGroup.Term</a>

<script>
  $("#ajaxLink").click(function () {     
    $.ajax({
      type: "POST",
      url: $("#Url").val(),
      data: { section : $("#Query").val() }
    }).done(function() {

    });
  });
</script>

【问题讨论】:

  • 您对查询字符串和 POST 数据感到困惑。首先决定你想通过什么,这是两件不同的事情。
  • 我想在 url 中添加一个查询字符串,例如:www.url.com/?section=value
  • 所以改成url: $("#Url").val() + "?section=value",

标签: jquery ajax asp.net-mvc query-string


【解决方案1】:

GETPOST 之间存在差异。

看看这个:What is the difference between POST and GET?

工作代码在这里:http://jsfiddle.net/felipemiosso/WtQsF/

javascript

$('#ajaxLink').click(function() {
    var $this = $(this);
    $this.attr('href', '?section=' + $('#query').val());
});

代码采用现有的href 并将其替换为所需的。也许你可能需要稍微适应一下……

【讨论】:

    【解决方案2】:

    您需要将类型:“POST”更改为类型:“GET”,以便传递查询字符串。

    【讨论】: