Click here to Skip to main content
15,906,816 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
i can't run ajax through my chrome via --disable-web-security, what should I do?
this is my code:
HTML
<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>
Posted
Updated 30-Apr-14 4:17am
v2
Comments
Kornfeld Eliyahu Peter 30-Apr-14 10:08am    
Explain yourself...Maybe some code and explanation of 'I can't' will help us understand what is your problem...
Coder93 30-Apr-14 10:36am    
I have added my code to my question.
Kornfeld Eliyahu Peter 30-Apr-14 11:41am    
Good, now explain what your problem, as I was able to run this code in Chrome with and without --disable-web-security flag!
Coder93 30-Apr-14 11:51am    
it always run "window.alert("Error "+ xmlhttp.statusText);"
Kornfeld Eliyahu Peter 30-Apr-14 11:53am    
It means you never get status 200. What status do you get?

1 solution

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...
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900