函数alertContents(){ if(http_request.readyState == 4){ if(http_request.status == 200){ //alert(http_request.responseText); result = http_request.r esponseText; document.getElementById(''myspan'')。innerHTML = result; }否则{ alert(''连接汇率服务器时出现问题。''); } } } < / script> < / head> < body> < br>< br> <输入类型= QUOT按钮"名称= QUOT按钮" value =" GET get.php?test = 2" onclick =" javascript :makeRequest(''http://www.webservicex.net/CurrencyConvertor.asmx/ConversionRate'',''?FromCurrency = GBP& ToCurrency = USD'');"> < br>< br> < br> ;< br> 服务器响应:< br> < span name =" myspan" id =" myspan">< / span> < / body> < / html> [/ html] 我已经在几个警告框中调试,所以我可以看到一些正在发生的事情。第一个ReadyState和ReadyState。警报框显示不同的结果,具体取决于脚本是在本地还是远程运行。如果它在本地运行,我得到ReadyState为1.当上传到我的主机帐户时,我得到ReadyState = 0. 关于这里可能发生什么的任何想法? /> 谢谢! Ron 解决方案 欢迎来到TSDN! 你能提供一个测试页面的链接吗? 当然。这是我的测试页面: http://www.tappingware.com/GetCurrencyRate.html 好的,我发现了这个问题。这是一个跨域问题。我不知道你可以(轻松)使用AJAX从第三方来源获取数据。我设置了一个应用程序代理来传递数据,现在一切正常。谢谢! I''ve put together a simple script that uses the XMLHttpRequest() function. It works when I run it locally, but when I upload it to my web hosting account, I never get a response back. Here is what I have:[html]<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN"><html lang="en"><head><script type="text/javascript" language="javascript"> var http_request = false; function makeRequest(url, parameters) { http_request = false; if (window.XMLHttpRequest) { // Mozilla, Safari,... http_request = new XMLHttpRequest(); if (http_request.overrideMimeType) { // set type accordingly to anticipated content type //http_request.overrideMimeType(''text/xml''); http_request.overrideMimeType(''text/html''); } } else if (window.ActiveXObject) { // IE try { http_request = new ActiveXObject("Msxml2.XMLHTTP"); } catch (e) { try { http_request = new ActiveXObject("Microsoft.XMLHTTP"); } catch (e) {} } } if (!http_request) { alert(''Cannot create XMLHTTP instance''); return false; } http_request.onreadystatechange = alertContents; http_request.open(''GET'', url + parameters, true);alert(url); http_request.setRequestHeader("User-Agent","XMLHttpRequest");alert(''ReadyState =''+http_request.readyState); http_request.send(null);alert(''sent the request'');alert(''ReadyState =''+http_request.readyState);alert(''Status =''+http_request.status); } function alertContents() { if (http_request.readyState == 4) { if (http_request.status == 200) { //alert(http_request.responseText); result = http_request.responseText; document.getElementById(''myspan'').innerHTML = result; } else { alert(''There was a problem connecting with the exchange rate server.''); } } }</script></head><body><br><br><input type="button" name="button" value="GET get.php?test=2" onclick="javascript:makeRequest(''http://www.webservicex.net/CurrencyConvertor.asmx/ConversionRate'', ''?FromCurrency=GBP&ToCurrency=USD'');"><br><br><br><br>Server-Response:<br><span name="myspan" id="myspan"></span></body></html>[/html]I''ve thrown in a few alert boxes for debugging, so I could see a few things that are going on. The first "ReadyState" alert box shows different results depending on whether the script is being run locally or remotely. If it is running locally, I get a ReadyState of 1. When uploaded to my hosting account, I get ReadyState=0.Any ideas about what may be going on here?Thanks!Ron 解决方案 Welcome to TSDN!Could you provide a link to a test page?Sure. Here''s my test page:http://www.tappingware.com/GetCurrencyRate.htmlOk, I found the issue. It was a cross-domain issue. I didn''t know that you can (easily) use AJAX to get data from a third-party source. I set up an application proxy to relay the data, and now everything is working. Thanks! 这篇关于AJAX在本地工作,但不在服务器上工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 上岸,阿里云!