我们可以在不将参数附加到url的情况下调用ajax get方法吗

时间:2019-03-03 06:52:21

标签: javascript jquery ajax rest

我知道在get方法中传递参数时,它将附加到url上。有没有办法在不附加URL的情况下在get方法中传递参数。 例如

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方法传递参数?

2 个答案:

答案 0 :(得分:4)

  

是否可以通过post方法传递参数?

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

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

答案 1 :(得分:0)

这是GET方法的功能。

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

相关问题