UPD1. I need to get the json file on the client’s machine, then it is processed and give the user the result.

UPD2. How to get data through HTTP on the client's machine using javascript.

  • Formulate the problem correctly. If the task is in uploading to a file page, for example, styles or a script file, then this solution, if the task is in use input type = file, then the other one. You might even think that you want to download the file directly, as @kirelagin wrote, then you can use ActiveX (for nothing else). - Alex Silaev
  • I need to get data through HTTP to the client’s machine using javascript. - psyhitus

2 answers 2

UPD1. I need to get the json file on the client’s machine, then it is processed and give the user the result.

Well, first, let's see what json is. Suppose that this is one of the species, i.e. set value pairs => property. We also assume that you have a JSON object called "test" for ease of understanding.

Then the problem is solved as follows:

var sc = document.createElement('script'); sc.src = 'путь к файлу, который отдаст json'; document.getElementsByTagName('head')[0].appendChild(sc); function readJson() { if (test) { // тут выполняем действия над объектом test } else { setTimeout(function() { readJson(); }, 1); } } readJson(); 
  • sc.src = 'the path to the file that json will give'; here you can specify localhost / example.json for example? - psyhitus
  • one
    For example - yes, if you have json on the path http: //.../something. - Alex Silaev
  • does the readJSON () function call itself until the file is loaded? - psyhitus
  • I completely forgot to add a line: document.getElementsByTagName ('head') [0] .appendChild (sc); Without it, the file will not connect. - Alex Silaev
  • > the readJSON () function calls itself until the file is loaded? Exactly, there is a delay in a call at 1 ms, since if the file does not load at all (for some reason, or the file does not give the desired test object), a kerdyk will come to the browser. - Alex Silaev

Do you want via JS to upload a file to the server from the user's computer? The browser will not let you do this for obvious reasons;).

You can make the usual form of sending a file to the page and manage it through JS (for example, make form.submit() ). But the user himself must initiate the file sending in any case.

  • I apologize, but you did not understand what I meant :-) - psyhitus