我如何使用 jQuery“加载"?执行带有额外参数的 GET 请求? [英] How can I use jQuery "load" to perform a GET request with extra parameters?
问题描述
我正在阅读 jQuery 加载文档,它提到我可以使用 load 来执行通过将额外参数作为字符串传递来获取 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 请求将由默认 - 但如果你传入任何额外的参数形式对象/映射(键/值对)然后一个POST 将发生.额外参数作为字符串传递仍将使用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屋!