阿贾克斯GET请求:使用参数或将数据网址是什么?或将、参数、网址、数据

2023-09-10 17:13:56 作者:对伱微笑╮纯属伱很可笑

什么是在Ajax GET请求传递数据作为参数VS URL的一部分的优势在哪里?

使用参数:

  VAR阿贾克斯=新的Ajax.Request('server.php',{
    参数:'商店= 11200&安培;产品=放心肉,
    的onSuccess:功能(myData的){}什么
});
 

使用网址:

  VAR阿贾克斯=新的Ajax.Request('?server.php店= 11200&安培;产品=肉',{
    的onSuccess:功能(myData的){}什么
});
 

解决方案

一个优点使用参数的说法是,你可以通过它散列样的对象,而不是作为一个字符串。 (如果你这样做,不过,要确保这样设置方法参数GET,作为默认的方法为原型Ajax请求是POST,请参见原型Ajax简介了解详情)

另外一个优势,这是更符合你给的例子,就是你可以从被发送给它的选项在不同的请求URL。如果,例如,你需要发送一串类似的请求,以多个不同的网址这可能是有用的。 (在这种情况下,具有共同参数散列您修改为每个请求可能更为有用,比使用的参数字符串,也是如此。)

HTTP协议笔记

有关详细信息,请参见阿贾克斯选项的原型文档。

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}
});

解决方案

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.