Can a local ajax request return corresponding data based on parameters like a php server? I now know that the webpage can get data in php through ajax requests, and can pass different data parameters to get different data through the same address, however, if the local json file can be obtained... can a local ajax request return corresponding data based on parameters like a php server?
I now know that the webpage can get data in php through ajax requests, and can pass different data parameters to get different data through the same address, but can the same effect be achieved if the local json file is obtained?
Reply content:
Can a local ajax request return corresponding data based on parameters like a php server?
I now know that the webpage can get data in php through ajax requests, and can pass different data parameters to get different data through the same address, but can the same effect be achieved if the local json file is obtained?
No. resources can be differentiated locally based on different URLs.
Different data is returned, which must be processed by the server and cannot be implemented without local environment. In fact, you can try using node to build a simple environment.
The problem description is not clear enough. what you mean by obtaining a local json file refers:xhr.open('post' , url , true)HereurlNot similar:php/test.phpSuch a server script istest.jsonSuch ??
If you do not use a server script such as php to schedule data, you can directly request the file content.
For example:
There are three json files:test.json , test1.json ,test2.json
Js: var path = 'Test. json '; // you only need to replace: test1.json | test2.json, and you can switch to different content xhr. open ('post', path, true); // xhr. setRequestHeader ('content-type', 'application/x-www-form-urlencoded'); xhr. send (); xhr. onreadystatechange = function () {if (this. readyState === 4 & this. status = 200) {console. log (this. responseText); // You can view test. data in json }}
If it is changed to php, the same effect will be achieved:
Js: var sendData = 'require = test'; // you only need to replace the parameter: require test1 | require = test2 to switch between different content xhr. open ('post', 'Test. php', true); xhr. setRequestHeader ('content-type', 'application/x-www-form-urlencoded'); xhr. send (sendData); xhr. onreadystatechange = function () {if (this. readyState === 4 & this. status = 200) {console. log (this. responseText); // You can view test. data in json} test. php: $ require = $ _ POST ['require ']; switch ($ require) {case 'test': require 'test. json '; exit; case 'test1': require 'test1. json '; exit ;.....}
In summary, I personally think php has added the processing function. Therefore, it is recommended that xhr send relevant parameters to php for processing and then feedback the data back, instead of directly requesting unprocessed raw text data.