我有一个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.