Jquery: Running AJAX locally without a webserver

前端 未结 7 780
孤独总比滥情好
孤独总比滥情好 2020-11-30 13:36

I have the following function in a .js file in index.html

function getValues(){

 $.ajax({
   type: \'POST\',
   url: \"http://localhost/getData/getdata.php\         


        
相关标签:
7条回答
  • 2020-11-30 14:12

    You can't do that, you should open your html file also from web server address eg http://localhost/yoursite/file.html or even remote server url. You need to go through the server/server url.

    0 讨论(0)
  • 2020-11-30 14:12

    Read the SOP. Accessing data from a domain other than the current one is blocked for security reasons.

    0 讨论(0)
  • 2020-11-30 14:21

    AJAX needs a webserver to communicate with for it to be able to retrieve any data; otherwise its just talking to a wall. Running the script without a webserver is like trying to make a call with no cell-service. :D

    0 讨论(0)
  • 2020-11-30 14:21

    Ajax does not work over the file:// protocol as mentioned by others. Perhaps you want something like http://www.appcelerator.com/ to create desktop apps with html/js/css

    0 讨论(0)
  • 2020-11-30 14:22

    I'm tickled pink with myself because reading the answers people posted about how you can't do AJAX "locally" without a Web server led me to the solution as to how you can do it. With JavaScript, the XMLHttpRequest() object's methods are mostly produced by the browser and you need to leave out the one produced by the Web server (xmlhttp.status == 200). The following works:

    <script>
    window.onload = function() {
    
        var input = document.getElementById("input");
    
        input.onclick = function() {
            var xmlhttp;
            xmlhttp = new XMLHttpRequest();
            xmlhttp.onreadystatechange = function() {
                if (xmlhttp.readyState == 4) {
                    document.getElementById("response").innerHTML = xmlhttp.responseText;
                }
            }
            xmlhttp.open("GET", "response.html", true);
            xmlhttp.send();
        }
    }
    </script>
    </head>
    
    <body>
    <h3>AJAX Request/Response</h3>
    <p></p>
    <input id="input" type="button" value="Call AJAX" />
    <p></p>
    <div id="response"></div>
    
    0 讨论(0)
  • 2020-11-30 14:25

    The web server is exactly what is handling all of the details for you.

    You cannot POST without a web server to post to. HTTP = web protocol, so you cannot have a HTTP URL without a web server to target.

    The web server is also the process that takes your request for a PHP page and runs the PHP interpreter, managing the inputs and outputs.

    Why do you want to run it locally?

    0 讨论(0)
提交回复
热议问题