admin管理员组文章数量:1390756
Making a GET request is easy, you just specify an URL in the href attribute of an anchor tag.
But, how do you add a JSON object to that URL?
I'm guessing this has to be done through jQuery... but I can't user AJAX, or the response isn't rendered by the browser.
so far, I've tried this: $j("#gen").attr("href", $j("#gen").attr("href") + "?" + JSON.stringify({object: data}));
but it doesn't append the data. This is triggered onClick
Making a GET request is easy, you just specify an URL in the href attribute of an anchor tag.
But, how do you add a JSON object to that URL?
I'm guessing this has to be done through jQuery... but I can't user AJAX, or the response isn't rendered by the browser.
so far, I've tried this: $j("#gen").attr("href", $j("#gen").attr("href") + "?" + JSON.stringify({object: data}));
but it doesn't append the data. This is triggered onClick
- 1 What do you mean you can't use AJAX or the response isn't rendered? Are you just trying to generate a link? – Josh Commented Feb 28, 2012 at 4:39
- yes, i'm just wanting to make an HTTP GET request so the server can redirect my browser, based on the data in teh params. – DerNalia Commented Feb 28, 2012 at 4:46
-
1
Strictly speaking, there is no need for jQuery, you just have to encode the JSON properly:
<a href="foo?bar={"some":"value"}">link</a>
. But I don't think you provide enough information (context) for your problem. – Felix Kling Commented Feb 28, 2012 at 4:49 - so far, I've tried this: $j("#gen").attr("href", $j("#gen").attr("href") + "?" + JSON.stringify({object: data})); but it doesn't append the data. This is triggered onClick – DerNalia Commented Feb 28, 2012 at 4:56
3 Answers
Reset to default 3Any values in a GET request are expected to be URL encoded into the request string.
There is a jQuery.get()
function that will do this for you, but all it does is serialize a set of name/value pairs as query string parameters.
I'm not really sure what you mean by not being able to use AJAX. Using jQuery you would do something like this:
$.get("foo/bar", { foo: "foo", bar: "bar" },
function(data){
$("#myTarget").append(data);
});
Assuming of course the return value was HTML.
Ok, I think I understand a little better. If you just want to turn some object into a Query String, then you can roll your own function pretty easy:
function toQueryString(obj){
var formattedVals = [];
for(var prop in obj){
formattedVals.push(prop + "=" + obj[prop]);
}
return formattedVals.join("&");
}
You can see how it would work by calling it this way:
var data = { foo: "foo", bar: "bar" };
alert(toQueryString(data));
Anyway, this should get you 90% of the way there.
A GET request has querystring params. If it's a GET request you really want, then you can map properties on the object to key value pairs on the request.
For example:
JSON object: { prop1: val1, prop2: val2 }
url get querystring: ?prop1=val1&prop2=val2
But, if you want to send an object to the server, perhaps you really want a POST? In that case, the JSON object would be in the body of the POST.
This should work:
jQuery("#gen").attr("href", jQuery("#gen").attr("href") + "?" + jQuery.param({object: data}));
本文标签: javascriptHow do I append a JSON object to a GET urlStack Overflow
版权声明:本文标题:javascript - How do I append a JSON object to a GET url? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744754017a2623351.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论