How can I use jQuery “load” to perform a GET request with extra parameters?

£可爱£侵袭症+ 提交于 2019-11-27 11:44:11

问题


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

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?


回答1:


According to the documentation you linked:

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.

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



回答2:


Use $.param(data):

$("#output").load(
    "server_output.html?" + $.param({
        year: 2009,
        country: "Canada"})
);



回答3:


$("#output").load("server_output.html?year=2009&country=Canada");



回答4:


can you not just do:

$("#output").load(
    "server_output.html?year=2009&country='Canada'"
);



回答5:


Use this

$("#output").load("server_output.html", {"2009":year, "Canada":country});


来源:https://stackoverflow.com/questions/1488593/how-can-i-use-jquery-load-to-perform-a-get-request-with-extra-parameters

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