我正在阅读jQuery load 文档,它提到我可以使用 load 通过将额外参数作为字符串传递来执行 GET 请求。我当前使用参数作为键/值对的代码是:
$("#output").load(
"server_output.html",
{
year: 2009,
country: "Canada"
}
);
以上工作正常,但这是一个发布请求。如何修改上述内容以在仍在使用的同时执行 GET 请求load
?
我正在阅读jQuery load 文档,它提到我可以使用 load 通过将额外参数作为字符串传递来执行 GET 请求。我当前使用参数作为键/值对的代码是:
$("#output").load(
"server_output.html",
{
year: 2009,
country: "Canada"
}
);
以上工作正常,但这是一个发布请求。如何修改上述内容以在仍在使用的同时执行 GET 请求load
?
使用$.param(data)
:
$("#output").load(
"server_output.html?" + $.param({
year: 2009,
country: "Canada"})
);
根据您链接的文档:
默认情况下将执行 GET 请求 - 但如果您以 Object/Map(键/值对)的形式传入任何额外参数,则会发生 POST。作为字符串传递的额外参数仍将使用 GET 请求。
因此,简单的解决方案是在将对象传递给函数之前将其转换为字符串。不幸的是,文档没有指定字符串应该采用的格式,但我猜这与您手动生成 GET 请求相同。
$("#output").load(
"/server_output.html?year=2009&country=Canada"
);
$("#output").load("server_output.html?year=2009&country=Canada");
你能不能只做:
$("#output").load(
"server_output.html?year=2009&country='Canada'"
);
用这个
$("#output").load("server_output.html", {"2009":year, "Canada":country});