如何从网址中打印出httpRequest.responseText [英] How do I print out httpRequest.responseText from a url

查看:250
本文介绍了如何从网址中打印出httpRequest.responseText的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

此操作的目的是在满足所有条件的情况下退出"httpRequest.responseText".如果未打印,则会显示错误消息.

就我而言,任何警告"或"httpRequest.responseText"上均未打印任何内容.
如何获得回应?

The object of this is to get out "httpRequest.responseText" if all the conditions are met. If not an error message is printed.

In my case nothing is printed on any of the "alerts" or "httpRequest.responseText".
How do I get a response?

<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<script type="text/javascript">
        (function () {
            function makeRequest(url) {
                if (window.XMLHttpRequest) { // Mozilla, Safari, ...
                    var httpRequest = new XMLHttpRequest();
                } else if (window.ActiveXObject) { // IE 8 and older
                    try {
                        var httpRequest = new ActiveXObject("Msxml2.XMLHTTP");
                    }
                    catch (e) {
                        try {
                            var httpRequest = new ActiveXObject("Microsoft.XMLHTTP");
                        }
                        catch (e) { }
                    }
                }

                if (!httpRequest) {
                    alert('Giving up :( Cannot create an XMLHTTP instance');
                    return false;
                }
                httpRequest.onreadystatechange = alertContents();
                httpRequest.open('GET', url);
                httpRequest.send();
            }

            function alertContents() {
                if (httpRequest.readyState === 4) {
                    if (httpRequest.status === 200) {
                        alert(httpRequest.responseText);
                    } else {
                        alert('There was a problem with the request.');
                    }
                }
            }
        })();
    </script>


<asp:TextBox id="TextBox1" runat="server">http://www.bing.com/</asp:TextBox>
<asp:Button id="Button1" runat="server" Text="URL"  OnClick="Button1_Click" />


Default.aspx.cs


Default.aspx.cs

protected void Button1_Click(object sender, EventArgs e)
{
    string url = TextBox1.Text;
    ScriptManager.RegisterClientScriptBlock(this, this.GetType(), "scr", "Javascript:makeRequest(" + url + ");", true);
}

推荐答案

您没有看到任何警告消息的原因是未调用您的函数.您已经注册了一个脚本来调用名为makeRequest的全局函数,但是该函数实际上是在另一个(匿名)函数中定义的,并且在该上下文之外不可用.

您已经在加载jQuery,因此请尝试使用内置的AJAX方法 [
The reason you''re not seeing any alert messages is that your function isn''t being called. You''ve registered a script to call a global function called makeRequest, but that function is actually defined inside another (anonymous) function, and is not available outside of that context.

You''re already loading jQuery, so try using the built-in AJAX methods[^]:
function makeRequest(url){


.get(url) .done(函数(数据){警报(数据);}) .fail(函数(){alert(' 请求有问题.');}); }
.get(url) .done(function(data){ alert(data); }) .fail(function(){ alert('There was a problem with the request.'); }); }


这篇关于如何从网址中打印出httpRequest.responseText的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆