我该如何使用jQuery的“加载"功能?执行带有额外参数的GET请求? [英] How can I use jQuery "load" to perform a GET request with extra parameters?
问题描述
我正在阅读 jQuery加载文档,其中提到可以使用加载来执行通过将额外的参数作为字符串传入GET请求.我当前使用参数作为键/值对的代码是:
I'm reading the jQuery load documentation and it mentions that I can use load to perform a GET request by passing in extra parameters as a string. My current code with my parameters as key/value pair is:
$("#output").load(
"server_output.html",
{
year: 2009,
country: "Canada"
}
);
上面的方法很好,但这是一个发布请求.如何在仍使用load
的情况下修改以上内容以执行GET请求?
The above works fine but it's a post request. How can I modify the above to perform a GET request while still using load
?
推荐答案
根据您链接的文档:
GET请求将由 默认-但是,如果您传入任何多余的内容 参数形式为 对象/地图(键/值对),然后 开机自检将发生.额外参数 作为字符串传递的仍将使用 GET请求.
A GET request will be performed by default - but if you pass in any extra parameters in the form of an Object/Map (key/value pairs) then a POST will occur. Extra parameters passed as a string will still use a GET request.
因此,简单的解决方案是在将对象传递给函数之前将其转换为字符串.不幸的是,文档没有指定字符串应采用的格式,但我想它与您手动生成GET请求的方式相同.
So the simple solution is to convert your object to a string before passing it to the function. Unfortunately, the documentation doesn't specify the format the string should be in, but I would guess it would be the same as if you were generating the GET request manually.
$("#output").load(
"/server_output.html?year=2009&country=Canada"
);
这篇关于我该如何使用jQuery的“加载"功能?执行带有额外参数的GET请求?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!