如何在chrome中运行ajax? [英] how to run ajax in chrome?

查看:161
本文介绍了如何在chrome中运行ajax?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我无法通过我的chrome通过--disable-web-security运行ajax,我该怎么办?

这是我的代码:

i can't run ajax through my chrome via --disable-web-security, what should I do?
this is my code:

<html>
<head>
<script type="text/javascript">

function runJsAsynch(url){
	var xmlhttp=new XMLHttpRequest();
	xmlhttp.onreadystatechange = process;
	xmlhttp.open("GET",url,true);
	xmlhttp.send(null);		
}

function process(){
	if(this.readyState == 4){
		if(this.status == 200){
			eval(this.responseText);
		}
		else{
			window.alert("Error "+ xmlhttp.statusText);
		}
	}
}

</script>
</head>
<body>
	<button type="button" onclick="runJsAsynch('msg1.js')">Alert Message</button>
	<button type="button" onclick="runJsAsynch('msg2.js')">HTML Message</button>
</body>
</html>

推荐答案

经过与OP的长时间讨论,我终于明白了什么问题是......

浏览某个网络服务器上托管的网页和用浏览器打开一些html文件之间存在巨大差异...

浏览时托管页面,浏览器使用 http https 协议加载内容,因此在调用msg1.js或msg2.js时它将使用正确的协议加载。

但是当你打开一个html文件(双击)时,浏览器使用 file 协议。当您尝试在代码中执行时,该协议不能用于执行 http (或 https )请求。 。

你在做什么是假设有一个网络服务器在某个地方为你服务,但没有!您只打开了一个文件,比如打开文本文档或图像文件。解决问题的唯一方法(因为您的代码很好)是使用一些可以为您处理Web开发的开发环境。这将为您提供浏览网页的正确方法,而不仅仅是打开它们......
After a long discussion with OP I finally understood what his problem...
There is a HUGE difference between browsing a web page hosted on some web server and opening some html file with a browser...
When browsing a hosted page, the browser uses http or https protocol to load content, so when calling msg1.js or msg2.js it will be load using the proper protocol.
However when you open an html file (double-click), the browser uses file protocol. That protocol can not be used to execute http (or https) request as you try to do in your code...
What are you doing is to assume that there is a web server somewhere to serve you, but there isn't! You only opened a file, like opening a text document or image file. The only way to solve your problem (as your code is fine) is to work with some development environment, that can handle web development for you. That will provide you with the proper way of browsing web pages and not just opening them...


这篇关于如何在chrome中运行ajax?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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