Resolve Ajax cannot access local files (using JS cross-domain principle) __js

Source: Internet
Author: User
Tags script tag
The blogger himself is developing a no backstage, only the front of the query system, encountered a difficulty, how to use Ajax to read their own local JSON file, Baidu, Google, found that most of the articles are said to modify browser parameters, but they think, users in use, but also to modify the browser parameters, This obviously lowers the user experience. I have been pondering for some time, I found that we can use JS Cross-domain principle to solve this bottleneck. will be read before the JSON file into a JS object file, so you can read this file, and then write a function to parse this JS object. first describes the next JS cross-domain principle and JSONP This technology (if not detailed, can own Baidu, Google)SRC can be requested across domains, but this resource can not be as JS parsing, resources are not JS, so parsing JSON will be an error in the middle of an object, call fun to pass this object in, defined function fun can be considered as a success after the request callback function JSONP essence: It is through the script tag Cross-domain, to break through the Cross-domain limit request; The returned data is wrapped in a function.
JSONP is used to solve the problem of JS Cross-domain request data, the principle is that through the script tag can request the resources in the domain of the principle of indirect request data.
Jsonp needs to meet 3 conditions: 1, Request resources 2 through the script SRC, request the resource to wrap the data in the callback function 3, the caller wants to define the callback function
below the demo to demonstrate Ajax read local files, PS: In the absence of a server
The following is an HTML page opening effect diagram above (to illustrate the successful reading of local files)


Paste in the test.html code above
<script type= "Text/javascript" src= "Js/jquery-easyui-1.3.2/jquery-1.8.0.min.js" > </script> <script> function Fun (data) {//var dataj=json.parse (Data.pro);//You can convert JSON strings to JSON
            Object alert (Data.pro); 
            var obj = eval (data.pro);
            alert (obj.name);
                        $.each (Obj,function (index,position) {//alert (+position[' name ')];
            html+= "<option value=" "+position[' name ']+" ' > "+position[" Name "]+" </option> ";
            //});
            for (Var i=0;i<obj.length;i++) {alert (obj[i].name);

        Alert (1); } </script> 
/* The following code, is the use of Ajax to read the local JSON file code, is not implemented, run, you can not paste the following code/
<script type= "Text/javascript" src= "Json/abc.json" "></script>
<script>
    $ (function () {
        alert ($);
        /*$.ajax ({
          type: "Get",
          URL: "Json/positionmenu.json",
          dataType: "JSON",
          success:function (data) {
                  alert (data);
            }
        ); */

    })
</script>



Related Article

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.