【问题标题】:Ajax - How to print to console or something similar to Message box?Ajax - 如何打印到控制台或类似于消息框的东西?
【发布时间】:2018-01-19 22:22:31
【问题描述】:

我正在使用谷歌浏览器上的开发者工具来解构一个网站。该网站使用 ajax 发送“发布”请求。我正在尝试修改 ajax 文件以在 ajax 文件中打印某些变量,但无法弄清楚如何。

代码如下:

      function ajax_doRefresh(url, type, params, onloadFunction){

var xmlHttp;
try
{    
    // Firefox, Opera 8.0+, Safari
    xmlHttp=new XMLHttpRequest();    
}
catch (e)
{   // Internet Explorer    
    try
    {      
        xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
    }
    catch (e)
    {      
        try
        {        
            xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");
        }
        catch (e)
        {        
            alert("Your browser does not support AJAX!");        
            return false;        
        }      
    }    
}

var result = null;

xmlHttp.onreadystatechange=function()
{
    if(xmlHttp.readyState==4)  
    {
        if (xmlHttp.status==200) 
        {
            if(type != null && type.toUpperCase() == "XML")
                result = xmlHttp.responseXML;   
            else
                result = xmlHttp.responseText;

            if(onloadFunction != null && onloadFunction.length > 0)
            {
                var exe_func = onloadFunction + "(result)";
                eval(exe_func);
            }
            //alert(result);
        }
        else
            alert("Error retrieving data: status code = " + xmlHttp.status);
    }
}    

xmlHttp.open("POST", url, true);
xmlHttp.setRequestHeader("Content-Type","application/x-www-form-urlencoded; charset=UTF-8");
xmlHttp.send(params);

}

我希望能够打印到控制台/查看 url、类型、参数、onloadFunction 的结果

【问题讨论】:

  • 使用console.log() 打印。

标签: javascript php ajax google-chrome


【解决方案1】:

这是一个重写,以使用 document.write 将文本输出到网页来替换状态 200 块。我还可以建议在 var xmlHttp 行之后插入 document.write('url: ' + url + ' type: ' + type + ' params: ' + params + '
');
.如给定的那样,我认为 onloadFunction 输出不会告诉你很多,但它可能会给出一些东西。您可能可以使用这样的东西来为您提供更多关于正在发生的事情的信息:

    if (xmlHttp.status==200) 
    {
        if(type != null && type.toUpperCase() == "XML")
            result = xmlHttp.responseXML;   
        else
            result = xmlHttp.responseText;

      document.write('result: ' + result + '<br>');

        if(onloadFunction != null && onloadFunction.length > 0)
        {
           document.write('onloadFunction: ' + onloadFunction + '<br>');
        }

    }

【讨论】:

    猜你喜欢
    • 2022-10-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-07-08
    • 2018-04-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多