XMLHttpRequest无法加载文件。只有HTTP支持跨源请求 [英] XMLHttpRequest cannot load file. Cross origin requests are only supported for HTTP

查看:357
本文介绍了XMLHttpRequest无法加载文件。只有HTTP支持跨源请求的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我得到以下错误:

  XMLHttpRequest无法加载file:/// C:/Users/richa.agiwal /Desktop/get/rm_Library/templates/template_viewSettings.html。只有HTTP支持跨源请求。 

我意识到这个问题已经被回答过了,但我仍然没有找到解决我的问题的方法。我尝试从命令提示符运行 chrome.exe --allow-file-access-from-files ,并将文件移动到本地文件系统,但我仍然得到同样的错误。



感谢任何建议!

在您的个人计算机的代码编辑器中编写HTML和Javascript,并在浏览器中测试输出时,您可能会收到有关跨源请求的错误消息。您的浏览器将呈现HTML并在您的浏览器中运行Javascript,jQuery,angularJs,而无需设置服务器。但许多网页浏览器都会编程以监视跨站点攻击,并会阻止请求。你不希望任何人都能从你的网络浏览器读取你的硬盘驱动器。您可以使用Notepad ++创建一个功能齐全的网页,该网页将运行Javascript以及jQuery和angularJs等框架;并使用Notepad ++菜单项 RUN,启动FIREFOX 来测试所有内容。这是开始创建网页的一种不错的简单方法,但是当您开始创建布局,CSS和简单页面导航之外的任何内容时,您需要在本机上设置本地服务器。



以下是我使用的一些选项。




  1. 在Firefox上本地测试您的网页,然后部署到您的主机。 / li>
  2. 或者:运行本地服务器



测试Firefox,部署到主机




  1. Firefox目前允许来自您硬盘驱动器的文件中的Cross Origin请求

  2. 您的网站托管网站将允许请求由清单文件配置的文件夹中的文件



运行本地服务器




  • 在计算机上运行服务器,如Apache或Python
  • Python不是服务器,但会运行简单的服务器



使用Python运行本地服务器



获取IP地址:




  • 在Windows上:打开'命令提示符'。所有程序,附件,命令提示符

  • 我总是以管理员身份运行命令提示符 。右键单击命令提示符菜单项并查找以管理员身份运行

  • 输入命令: ipconfig ,然后按Enter。

  • 寻找:IPv4地址。 。 。 。 。 。 。 。 12.123.123.00
  • 有些网站也会显示你的IP地址



如果你不喜欢没有Python,请下载并安装它。



使用命令提示符,您必须转到您想要用作网页的文件所在的文件夹。


  • 如果您需要返回到C:\根目录 - 键入cd /

  • 键入cd Drive:\Folder\Folder\etc进入您的.Html文件所在的文件夹(或php等)。

  • 检查路径。在命令提示符下键入:path。您必须看到python所在文件夹的路径。例如,如果python位于C:\Python27中,那么您必须在列出的路径中看到该地址。

  • 如果Python目录的路径不在路径中,你必须设定路径。键入:help path并按Enter。您将看到有关路径的帮助。
  • 输入如下所示的内容:path c:\python27%path%

  • %path%保留所有当前路径。您不想删除所有当前路径,只需添加一个新路径。 从要提供文件的文件夹中创建新路径。
  • b $ b
  • 启动Python服务器:输入: python -m SimpleHTTPServer port 其中'port'是所需端口号,例如 python -m SimpleHTTPServer 1337

  • 如果将端口留空,则默认为端口8000

  • Python服务器启动成功,您将看到一个消息。



本地运行Web应用程序




  • 打开浏览器

  • 在地址栏中键入: http://您的IP地址:port http://xxx.xxx.xx:1337 http:// xx .xxx.xxx.xx:8000 作为默认值

  • 如果服务器正在运行,您将在浏览器中看到您的文件列表

  • 点击您想要投放的文件,并显示该文件。



更多广告vanced解决方案




  • 安装集成的代码编辑器,Web服务器和其他服务。 >

    您可以在机器上单独安装Apache,PHP,Python,SQL,调试器等,然后花费大量时间试图弄清楚如何使它们一起工作,或者寻找将所有这些内容结合在一起的解决方案。



    我喜欢在NetBeans IDE中使用XAMPP。您还可以安装提供用户界面的WAMP,以管理和集成Apache及其他服务。


    I am getting the following error:

    XMLHttpRequest cannot load file:///C:/Users/richa.agiwal/Desktop/get/rm_Library/templates/template_viewSettings.html. Cross origin requests are only supported for HTTP. 
    

    I realize that this question has been answered before, but I still have not found a solution to my problem. I tried running chrome.exe --allow-file-access-from-files from the command prompt, and moved the file to the local file system, but I still get the same error.

    I appreciate any suggestions!

    解决方案

    If you are doing something like writing HTML and Javascript in a code editor on your personal computer, and testing the output in your browser, you will probably get error messages about Cross Origin Requests. Your browser will render HTML and run Javascript, jQuery, angularJs in your browser without needing a server set up. But many web browsers are programed to watch for cross site attacks, and will block requests. You don't want just anyone being able to read your hard drive from your web browser. You can create a fully functioning web page using Notepad++ that will run Javascript, and frameworks like jQuery and angularJs; and test everything just by using the Notepad++ menu item, RUN, LAUNCH IN FIREFOX. That's a nice, easy way to start creating a web page, but when you start creating anything more than layout, css and simple page navigation, you need a local server set up on your machine.

    Here are some options that I use.

    1. Test your web page locally on Firefox, then deploy to your host.
    2. or: Run a local server

    Test on Firefox, Deploy to Host

    1. Firefox currently allows Cross Origin Requests from files served from your hard drive
    2. Your web hosting site will allow requests to files in folders as configured by the manifest file

    Run a Local Server

    • Run a server on your computer, like Apache or Python
    • Python isn't a server, but it will run a simple server

    Run a Local Server with Python

    Get your IP address:

    • On Windows: Open up the 'Command Prompt'. All Programs, Accessories, Command Prompt
    • I always run the Command Prompt as Administrator. Right click the Command Prompt menu item and look for Run As Administrator
    • Type the command: ipconfig and hit Enter.
    • Look for: IPv4 Address . . . . . . . . 12.123.123.00
    • There are websites that will also display your IP address

    If you don't have Python, download and install it.

    Using the 'Command Prompt' you must go to the folder where the files are that you want to serve as a webpage.

    • If you need to get back to the C:\ Root directory - type cd/
    • type cd Drive:\Folder\Folder\etc to get to the folder where your .Html file is (or php, etc)
    • Check the path. type: path at the command prompt. You must see the path to the folder where python is located. For example, if python is in C:\Python27, then you must see that address in the paths that are listed.
    • If the path to the Python directory is not in the path, you must set the path. type: help path and hit Enter. You will see help for path.
    • Type something like: path c:\python27 %path%
    • %path% keeps all your current paths. You don't want to wipe out all your current paths, just add a new path.
    • Create the new path FROM the folder where you want to serve the files.
    • Start the Python Server: Type: python -m SimpleHTTPServer port Where 'port' is the number of the port you want, for example python -m SimpleHTTPServer 1337
    • If you leave the port empty, it defaults to port 8000
    • If the Python server starts successfully, you will see a msg.

    Run You Web Application Locally

    • Open a browser
    • In the address line type: http://your IP address:port
    • http://xxx.xxx.x.x:1337 or http://xx.xxx.xxx.xx:8000 for the default
    • If the server is working, you will see a list of your files in the browser
    • Click the file you want to serve, and it should display.

    More advanced solutions

    • Install a code editor, web server, and other services that are integrated.

    You can install Apache, PHP, Python, SQL, Debuggers etc. all separately on your machine, and then spend lots of time trying to figure out how to make them all work together, or look for a solution that combines all those things.

    I like using XAMPP with NetBeans IDE. You can also install WAMP which provides a User Interface for managing and integrating Apache and other services.

    这篇关于XMLHttpRequest无法加载文件。只有HTTP支持跨源请求的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆