简体   繁体   English

如何将json数据从一个JavaScript传递到另一个

[英]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. 这意味着, 由于您说的数据是相同的 ,因此您可以执行与以前完全相同的操作。

  1. Keep a reference to this 保留this的参考
  2. Make your async call 拨打异步电话
  3. 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.

相关问题 如何将json数据从一个JavaScript函数传递到另一个JavaScript函数? - How to pass json data from one javascript function to another javascript function? 如何将数据从一个Javascript控制器文件传递到另一个? - How to pass data from one Javascript controller file to another? 将Javascript数据从一个函数传递到另一个函数 - Pass Javascript data from one function to another javascript - 将数据从一个函数传递到另一个函数 - javascript - pass data from one function to another 如何将json数组对象从一个JavaScript适配器传递到另一个JavaScript适配器? - How to pass json array object from one javascript adapter to another javascript adapter? 如何将数据从一个功能传递到另一个功能? - how to pass data from one to another function of? 如何将数据从javascript传递到json - how to pass data from javascript to json 如何将JSON数据从控制器传递到JavaScript - How to pass json data from the controller to javascript 如何在没有PHP的情况下用PHP和Javascript将数据从一页传递到另一页? - How can I pass data from one page to another in PHP and Javascript without from? 如何将特定的JSON数据从一个工厂传递到Angular JS中的另一个工厂? - How to pass specific JSON data from one factory to another factory in Angular JS?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM