douzhoulei8959 2014-12-01 07:59
浏览 80
已采纳

如何在同一个html页面上进行多个ajax调用?

I have the following html page welcoming the user. I intend to make one call to /register once and then to /login once through ajax calls. But when this page renders, clicking the buttons does nothing and the functions showdata() and reg_user() are not even called. What could be the problem? Chrome developer tool says the following

XMLHttpRequest cannot load https: //127.0.0.1/login. No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http: //127.0.0.1' is therefore not allowed access.

But I am not making any Cross domain calls. I'm calling to the same localhost domain after all. Please elucidate what is going on. I'm runnning Apache/Ubuntu/PHP on the server

<html>
<title> Welcome </title>
<script>
function makeRequestObject(){
   var xmlhttp=false;
   try {
      xmlhttp = new ActiveXObject('Msxml2.XMLHTTP');
   } catch (e) {
      try {
         xmlhttp = new ActiveXObject('Microsoft.XMLHTTP');
      } catch (E) {
         xmlhttp = false;
      }
   }
   if (!xmlhttp && typeof XMLHttpRequest!='undefined') {
      xmlhttp = new XMLHttpRequest();
   }
   return xmlhttp;
}

function showdata()
{
   alert("show data called");
   var xmlhttp=makeRequestObject();
   var user=document.getElementById('user_name').value;
   var pass=document.getElementById('password').value;
   var url = 'https://127.0.0.1/login';
   var params = 'uname='+user+'&'+'pass='+pass;

   xmlhttp.open('POST', url, true);
   xmlhttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");

   xmlhttp.onreadystatechange=function() {
      if (xmlhttp.readyState==4 && xmlhttp.status == 200) {
         var content = xmlhttp.responseText;
         if( content ){
            document.getElementById('userdiv').innerHTML = content;
         }
      }
   }
   xmlhttp.send(params);
}
function reg_user()
{
   alert("reg_user called");
   var xmlhttp=makeRequestObject();
   var user=document.getElementById('reg_username').value;
   var pass=document.getElementById('reg_password').value;
   var url = 'https://127.0.0.1/register';
   var params = 'uname='+user+'&'+'pass='+pass;

   xmlhttp.open('POST', url, true);
   xmlhttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded"$

   xmlhttp.onreadystatechange=function() {
      if (xmlhttp.readyState==4 && xmlhttp.status == 200) {
         var content = xmlhttp.responseText;
         if( content ){
            document.getElementById('regdiv').innerHTML = content;
         }
      }
   }
   xmlhttp.send(params);
}
</script>
<body>
Enter user name : <input type="text" id="user_name" /><br/>
Enter your password : <input type="text" id="password"/><br/>
<input type="button" onclick="showdata()" value="Submit"><br/><br/>
<div id="userdiv">
</div>
<br />
Registration
<br />
Enter user name: <input type="text" id="reg_username" /><br/>
Enter your password : <input type="text" id="reg_password"/><br/>
<input type="button" onclick="reg_user()" value="Submit"><br/><br/>
<div id="regdiv">
</div>
</body>
</html>
  • 写回答

1条回答 默认 最新

  • dongzituo5530 2014-12-01 08:14
    关注

    The error you're receiving is caused by CORS validation. Ensure that the server https://127.0.0.1/sets the proper HTTP response headers per the CORS specification.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 下图接收小电路,谁知道原理
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度
  • ¥30 关于#r语言#的问题:如何对R语言中mfgarch包中构建的garch-midas模型进行样本内长期波动率预测和样本外长期波动率预测
  • ¥15 ETLCloud 处理json多层级问题
  • ¥15 matlab中使用gurobi时报错
  • ¥15 这个主板怎么能扩出一两个sata口
  • ¥15 不是,这到底错哪儿了😭
  • ¥15 2020长安杯与连接网探