【问题标题】:Can we call ajax get method without appending parameters to url我们可以在不向url附加参数的情况下调用ajax get方法吗
【发布时间】:2019-03-03 06:52:21
【问题描述】:

我知道在get方法中传递参数时,它会附加到url。有没有办法在get方法中传递参数而不附加到URL。 例如

function ajaxgetCall()
{
$.ajax({
  url: "http://test.com",
  type: "get", //send it through get method
  data: { 
    UserID: "test", 
    EmailAddress: "test@test.test"
  },
  success: function(response) {
    console.log("Sucess");
  },
  error: function(xhr) {
    console.log("Error");
  }
});
};
ajaxgetCall();
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>

所以这里的网址是,

http://test.com/?UserID=test&EmailAddress=test%40test.test

我想要什么:

http://test.com/

是否可以像我们在post方法中一样传递参数。

【问题讨论】:

  • 有什么理由不使用POST 方法?

标签: javascript jquery ajax rest


【解决方案1】:

是否可以像我们在post方法中一样传递参数。

如果您的意思是在正文中,则否,因为 GET 方法不允许正文。 MDN

但您可以使用标题,例如 $.ajax({ headers: {UserID: "test"}}) 在这种情况下,您需要修改服务器代码以从标头中提取数据。

【讨论】:

    【解决方案2】:

    这是GET方法的特点。

    如果您不想将属性附加到 URL,则必须使用 POST 方法。

    【讨论】:

      猜你喜欢
      • 2011-04-09
      • 2018-08-20
      • 2011-08-23
      • 1970-01-01
      • 2012-10-03
      • 1970-01-01
      • 2020-12-12
      • 2013-06-11
      • 1970-01-01
      相关资源
      最近更新 更多