你可以通过一个URL发送JSON? [英] Can you send JSON through a url?

查看:186
本文介绍了你可以通过一个URL发送JSON?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个Ruby散列,其中键是URL和值是整数。我转换哈希JSON和我想知道如果我将能够通过Ajax请求发送JSON一个URL里面,然后拉了JSON从params散列,。

I have a ruby hash where the keys are urls and the values are integers. I convert the hash to JSON and I'm wondering if I'll be able to send the JSON inside a url via an AJAX request and then pull that JSON from a params hash.

此外,我将要发送JSONifyed红宝石哈希返回给客户端。如果我有一个成功的回调在我的AJAX功能,我接收到的数据在数据变量,我怎么解析JSON与jQuery?

Also, I am going to be sending a JSONifyed ruby hash back to the client. If I have a success callback in my AJAX function where I receive the data in a data variable, how do I parse that JSON with JQuery?

请让我知道如果我需要更加具体。

Please let me know if I need to be more specific.

推荐答案

是的,你可以没有问题。无需手动编码/解码需要!

Yes, you can with no problem. No manual encoding/decoding needed!

您code会是这样的:

Your code would be like this:

var jsonParam = '{"name":"Edgar"}'; //Sample json param
$.ajax({
  ...  
  type: "get", //This sends in url
  data: {jsonParam: jsonParam}, //This will encode your json for url automatically
  dataType: "json", //With this the response will be automatically json-decoded!
  success: function(response){ //Assuming your server output was '{"lastName":"Villegas"}' as string
     alert(response.lastName);
  }
});

正如你所看到的,是不需要手动编码/解码。 jQuery的处理这一切!

As you can see, no manual encoding/decoding was needed. Jquery handles it all!

希望这有助于。干杯

PS:如果由于某种原因,你需要连接C本JSON code /德$ C $手动为URL中使用JavaScript的连接codeURIComponent(字符串) $。parseJSON(jsonString)的方法。

PS: If, for some reason, you need to encode/decode your json manually for url use javascript's encodeURIComponent(string) and $.parseJSON(jsonString) methods.

这篇关于你可以通过一个URL发送JSON?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆