[英]How to pass json data from one javascript to another
Guys here the first script is designed to take static .json files to display certain content and animate them. 伙计们,这里的第一个脚本旨在获取静态.json文件以显示某些内容并为其设置动画。 The code snippet is
该代码段是
var self = this;
$.getJSON('data/post_'+ index +'.json', function(d){
self.postCache[index] = d;
callback(d)
but I want to modify it to so it could take data from database. 但我想对其进行修改,以便可以从数据库中获取数据。 I got the data in the same format as it was in static json files and my question is how can I pass that data to this script so that it work as it should.
我得到的数据格式与静态json文件中的格式相同,我的问题是如何将这些数据传递给此脚本,以使其能够正常工作。
I am sending request using ajax 我正在使用ajax发送请求
$(function ()
{
$.ajax({
url: 'api.php',
data: "<?php echo $data?>",
dataType: 'json',
success: function(data)
{
//I really dont know what to write here :(
}
});
});
and the api.php file is getting data from database and I an encoding the data to json using json_encode 和api.php文件正在从数据库中获取数据,我使用json_encode将数据编码为json
$.getJSON
just calls $.ajax
with some predefined values automatically set for you. $.getJSON
只会调用$.ajax
并自动为您设置一些预定义的值。 This means, since you said the data is the same , that you can do exactly the same as you were doing previously. 这意味着, 由于您说的数据是相同的 ,因此您可以执行与以前完全相同的操作。
this
this
的参考 in the success function, save your reference and execute your callback 在成功函数中,保存您的引用并执行回调
var self = this; $.ajax({ url: 'api.php', data: "<?php echo $data?>", dataType: 'json', success: function(data) { self.postCache[index] = data; callback(data); } });
var self = this;
$.post('api.php',
{"post_id": index},
success: function(d)
{
self.postCache[index] = d;
callback(d);
});
see http://api.jquery.com/jquery.post/ 参见http://api.jquery.com/jquery.post/
In api.php do a SQL query where the post_id = $_POST['post_id'] then echo json_encode($database_row); 在api.php中执行SQL查询,其中post_id = $ _POST ['post_id']然后回显json_encode($ database_row);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.