weixin_33670713 2016-02-23 18:47 采纳率: 0%
浏览 5

难以获得回应

I'm having some troubles trying to execute an ajax call. It is stored in chat.js (added in the html head) and it does a call to getChatHistory.php

chat.js:

function getChatHistory(user1, user2){  
var response = 'fail';
var xmlhttp = new XMLHttpRequest();

xmlhttp.onreadystatechange = function()
{
    if (xmlhttp.readyState == 4 && xmlhttp.status == 200)
    {
        response = response + xmlhttp.responseText;
    } else {
        response = "Error:" + hmlhttp.status;
    }

    xmlhttp.open('GET', 'getChatHistory.php?user1=' + user1 + '&user2=' + user2);
    xmlhttp.send();
}
return response;}

getChatHistory.php:

<?php
echo "the php talks";
?>

index.html:

<script>
(function(){
  alert(getChatHistory('user1', 'user2');
})()

I checked with alert() and the onreadystatechange event doesn't work.

  • 写回答

1条回答 默认 最新

  • weixin_33712881 2016-02-23 18:52
    关注

    You're not sending the request due to the fact that your .open and .send functions are inside of your callback, try this instead:

    function getChatHistory(user1, user2){  
        var response = 'fail';
        var xmlhttp = new XMLHttpRequest();
    
        xmlhttp.onreadystatechange = function()
        {
            if (xmlhttp.readyState == 4 && xmlhttp.status == 200)
            {
                response = response + xmlhttp.responseText;
            } else {
                response = "Error:" + hmlhttp.status;
            }
        }
    
        xmlhttp.open('GET', 'getChatHistory.php?user1=' + user1 + '&user2=' + user2);
        xmlhttp.send();
        return response;
    }
    

    Note, that you are also going to run into issues getting the response to return due to the fact that it's an async request. The response will return undefined unless you either a) make it a synchronous request (generally a bad idea) or b) set your action requiring the response to fire after the ready state has completed. Here is a basic example of how you could do so:

    function getChatHistory(user1, user2, onComplete){  
        var response = 'fail';
        var xmlhttp = new XMLHttpRequest();
    
        xmlhttp.onreadystatechange = function()
        {
            if (xmlhttp.readyState == 4 && xmlhttp.status == 200)
            {
                response = response + xmlhttp.responseText;
            } else {
                response = "Error:" + hmlhttp.status;
            }
    
            onComplete(response);
        }
    
        xmlhttp.open('GET', 'getChatHistory.php?user1=' + user1 + '&user2=' + user2);
        xmlhttp.send();
    }
    

    index.html

    <script>
    (function(){
      getChatHistory('user1','user2', function(resp){
         alert(resp);
      });
    })();
    </script>
    
    评论

报告相同问题?

悬赏问题

  • ¥15 echarts动画效果失效的问题。官网下载的例子。
  • ¥60 许可证msc licensing软件报错显示已有相同版本软件,但是下一步显示无法读取日志目录。
  • ¥15 Attention is all you need 的代码运行
  • ¥15 一个服务器已经有一个系统了如果用usb再装一个系统,原来的系统会被覆盖掉吗
  • ¥15 使用esm_msa1_t12_100M_UR50S蛋白质语言模型进行零样本预测时,终端显示出了sequence handled的进度条,但是并不出结果就自动终止回到命令提示行了是怎么回事:
  • ¥15 前置放大电路与功率放大电路相连放大倍数出现问题
  • ¥30 关于<main>标签页面跳转的问题
  • ¥80 部署运行web自动化项目
  • ¥15 腾讯云如何建立同一个项目中物模型之间的联系
  • ¥30 VMware 云桌面水印如何添加