Ajax GET requests: use parameters or put data in URL?

落花浮王杯 提交于 2020-01-13 08:11:08

问题


What's the advantage of passing data as parameters vs part of the URL in an Ajax GET request?

Using parameters:

var ajax = new Ajax.Request('server.php',{
    parameters: 'store=11200&product=Meat',
    onSuccess: function(myData){whatever}
});

Using URL:

var ajax = new Ajax.Request('server.php?store=11200&product=Meat',{
    onSuccess: function(myData){whatever}
});

回答1:


One advantage to using the parameters argument is that you can pass it a Hash-like object instead of as a string. (If you do this, though, make sure so set the method parameter to "GET", as the default method for Prototype Ajax requests is POST; see the Prototype Introduction to Ajax for more details.)

Another advantage, which is more in-line with the example that you gave, is that you can separate the request URL from the options that are sent to it. This might be useful if, for example, you need to send a bunch of similar requests to several different URLs. (In that case, having a common parameters Hash that you modify for each request might be more useful, than using a parameter string, as well.)

For more information, see the Prototype documentation of Ajax options.




回答2:


One of my favorite uses of parameters is to pass in all fields of a form without explicitly listing them:

new Ajax.Request('/myurl.php', {
  method:  'get',
  parameters:  $('myForm').serialize(),
  onSuccess:  successFunc(),
  onFailure:  failFunc()
}



回答3:


To answer this, you should know the way the parameters work. HTTP basically (I know, there are more) has two methods to request data: GET and POST.

For GET, parameters are appended to the resource you request, like you did in your code above: /my/resource/name?para1=bla. Here, there is no difference if you append if directly to the resource name or use the parameters option. GET is normally used to request data (Its GET ;)

For POST, the parameters are written seperate from the resource in the HTTP body. For this, you must use the parameters option. POST is used to send (huge) data.

To specify which request method to use, use the method option.

Note: The GET resource has (depending from server to server) a hard limit on the length. So NEVER send much data using GET.




回答4:


You can also use the format:

var ajax = new Ajax.Request('server.php',{
  parameters: {
     store: 11200,
     product: "Meat"
  }
  onSuccess: function(myData){whatever}
});

On advantage of doing it this way is that you can change from a GET to a POST without changing the URL.




回答5:


  • Legibility
  • Easy to use a object and serialise it ( {store: 11200, product: "Meat"})
  • Legibility



回答6:


It doesn't really matter from a technical standpoint on this other than formatting and preference because get requests always have the data in the URL. The parameters are just a convenient way of building the GET request.



来源:https://stackoverflow.com/questions/218399/ajax-get-requests-use-parameters-or-put-data-in-url

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!