Javascript-can a local ajax request return corresponding data based on parameters like a php server ??

Source: Internet
Author: User
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.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.