如何使用JSONP下载客户端javascript对象?

时间:2021-07-19 16:19:15

I'm trying to get client-side javascript objects saved as a file locally. I'm not sure if this is possible.

我正在尝试将客户端javascript对象保存为本地文件。我不确定这是否可能。

The basic architecture is this:

基本架构是:

  1. Ping an external API to get back a JSON object
  2. Ping一个外部API以返回一个JSON对象。
  3. Work client-side with that object, and eventually have a "download me" link
  4. 使用该对象进行客户端工作,最终得到一个“下载我”链接
  5. This link sends the data to my server, which processes it and sends it back with a mime type application/json, which (should) prompt the user to download the file locally.
  6. 这个链接将数据发送到我的服务器,服务器对数据进行处理并使用mime类型的应用程序/json将数据发送回来,这将提示用户在本地下载文件。

Right now here are my pieces:

下面是我的作品:

Server Side Code

服务器端代码

<?php
$data = array('zero', 'one', 'two', 'testing the encoding');  
$json = json_encode($data);  
//$json = json_encode($_GET['']); //eventually I'll encode their data, but I'm testing
header("Content-type: application/json"); 
header('Content-Disposition: attachment; filename="backup.json"'); 
echo $_GET['callback'] . ' (' . $json . ');';
?>

Relevant Client Side Code

有关客户端代码

$("#download").click(function(){
    var json = JSON.stringify(collection); //serializes their object
    $.ajax({
       type: "GET",
       url: "http://www.myURL.com/api.php?callback=?", //this is the above script
       dataType: "jsonp",
       contentType: 'jsonp',
       data: json,
       success: function(data){
           console.log( "Data Received: " + data[3] );
       }
    });
    return false;
});

Right now when I visit the api.php site with Firefox, it prompts a download of download.json and that results in this text file, as expected:

当我访问api时。php网站与Firefox,它提示下载。这将导致这个文本文件,如预期的那样:

 (["zero","one","two","testing the encoding"]);

And when I click #download to run the AJAX call, it logs in Firebug

当我点击#download运行AJAX调用时,它会登录到Firebug中

Data Received: testing the encoding

which is almost what I'd expect. I'm receiving the JSON string and serializing it, which is great. I'm missing two things:

这几乎是我所期望的。我正在接收JSON字符串并将其序列化,这很棒。我失踪的两件事:

The Actual Questions

实际的问题

  1. What do I need to do to get the same prompt-to-download behavior that I get when I visit the page in a browser
  2. 当我在浏览器中访问页面时,我需要做什么来获得相同的提示-下载行为
  3. (much simpler) How do I access, server-side, the json object being sent to the server to serialize it? I don't know what index it is in the GET array (silly, I know, but I've tried almost everything)
  4. (更简单)如何访问服务器端json对象并将其序列化?我不知道GET数组中的索引是什么(我知道这很傻,但我几乎尝试了所有的方法)

1 个解决方案

#1


2  

  1. You need to tell the browser to visit the page, usually by setting window.location.
  2. 您需要告诉浏览器访问页面,通常是通过设置window.location。
  3. Since it's a string, it will be sent as part of the raw query string. Try looking in $_SERVER['QUERY_STRING'] for it.
  4. 由于它是一个字符串,它将作为原始查询字符串的一部分发送。尝试在$_SERVER['QUERY_STRING']中查找。

#1


2  

  1. You need to tell the browser to visit the page, usually by setting window.location.
  2. 您需要告诉浏览器访问页面,通常是通过设置window.location。
  3. Since it's a string, it will be sent as part of the raw query string. Try looking in $_SERVER['QUERY_STRING'] for it.
  4. 由于它是一个字符串,它将作为原始查询字符串的一部分发送。尝试在$_SERVER['QUERY_STRING']中查找。