How to download silently with javascript

Deadly 提交于 2019-12-23 07:12:26

问题


I am new in javascript and I have some trouble to complete an activity...

I need working, in the client side, with the information uploaded in a "special" server like this:

http://www.uninorte.edu.co/documents/71051/11558879/ExampleData.json/0a635cdd-ccdd-4a1c-8c88-b53bea431458

I want load it in main memory, without the browser show the explicit download.

I try to use some solutions, but really I have no idea how to proceed to achieve it.

... Beforehand thank you very much

(I am not a native english speaker, I apologize if I do not write well)

[Solved]

I decided, for the moment, use the Whatever Origin services, that returns me something I can read with $.getJSON "Without download" the file.

Resulted:

<script type="text/javascript">
      $.getJSON('http://whateverorigin.org/get?url=' + "http://www.uninorte.edu.co/documents/71051/11558879/ExampleData.json/0a635cdd-ccdd-4a1c-8c88-b53bea431458" + '&callback=?', function(data){
        alert(data.contents);
      });
 </script>

Thank you for yours responses, really you gave me lights in order to solve it


回答1:


Regarding silent part

Your browser is always aware of the network requests which are made from JS. Therefore, the user can always see all the requests and responses by opening developer tools.


Now coming to loading a remote json to the memory in the client

Since you mentioned you are relatively a newbie in JS, I am going to cover the very basic, so please bear with me if you already know it

You need to make an ajax call using an XMLHttpRequest as shown here


However, most people use some library like jQuery while working to abstract checking state of the request and other trivial tasks. This results in making an ajax call as simple as calling a method and providing a callback method to process the response.

$.ajax({
  url: '/path/to/file',
  type: 'default GET (Other values: POST)',
  dataType: 'default: Intelligent Guess (Other values: xml, json, script, or html)',
  data: {param1: 'value1'},
})
.done(function() {
  console.log("success");
})
.fail(function() {
  console.log("error");
})
.always(function() {
  console.log("complete");
});

You may find the example at below link.

http://api.jquery.com/jquery.getjson/

P.S.: Due the less reputation points, I can neither post any supporting images nor more than two links.



来源:https://stackoverflow.com/questions/37781938/how-to-download-silently-with-javascript

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!