Cross origin requests are only supported for HTTP but it's not cross-domain

后端 未结 9 1684
花落未央
花落未央 2020-11-22 08:54

I\'m using this code to make an AJAX request:

$(\"#userBarSignup\").click(function(){
    $.get(\"C:/xampp/htdocs/webname/resources/templates/signup.php\",
          


        
相关标签:
9条回答
  • 2020-11-22 09:15

    I've had luck starting chrome with the following switch:

    --allow-file-access-from-files
    

    On os x try (re-type the dashes if you copy paste):

    open -a 'Google Chrome' --args -allow-file-access-from-files
    

    On other *nix run (not tested)

     google-chrome  --allow-file-access-from-files
    

    or on windows edit the properties of the chrome shortcut and add the switch, e.g.

     C:\ ... \Application\chrome.exe --allow-file-access-from-files
    

    to the end of the "target" path

    0 讨论(0)
  • 2020-11-22 09:15

    It works best this way. Make sure that both files are on the server. When calling the html page, make use of the web address like: http:://localhost/myhtmlfile.html, and not, C::///users/myhtmlfile.html. Make usre as well that the url passed to the json is a web address as denoted below:

    $(function(){
                    $('#typeahead').typeahead({
                        source: function(query, process){
                            $.ajax({
                                url: 'http://localhost:2222/bootstrap/source.php',
                                type: 'POST',
                                data: 'query=' +query,
                                dataType: 'JSON',
                                async: true,
                                success: function(data){
                                    process(data);
                                }
                            });
                        }
                    });
                });
    
    0 讨论(0)
  • 2020-11-22 09:25

    I was getting the same error while trying to load simply HTML files that used JSON data to populate the page, so I used used node.js and express to solve the problem. If you do not have node installed, you need to install node first.

    1. Install express npm install express

    2. Create a server.js file in the root folder of your project, in my case one folder above the files I wanted to server

    3. Put something like the following in the server.js file and read about this on the express gihub site:

      var express = require('express');
      var app = express();
      var path = require('path');
      
      // __dirname will use the current path from where you run this file 
      app.use(express.static(__dirname));
      app.use(express.static(path.join(__dirname, '/FOLDERTOHTMLFILESTOSERVER')));
      
      app.listen(8000);
      console.log('Listening on port 8000');
      
    4. After you've saved server.js, you can run the server using:

    node server.js

    1. Go to http://localhost:8000/FILENAME and you should see the HTML file you were trying to load
    0 讨论(0)
提交回复
热议问题