Click here to Skip to main content
15,886,110 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
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?
JavaScript
<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.NET
<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
C#
protected void Button1_Click(object sender, EventArgs e)
{
    string url = TextBox1.Text;
    ScriptManager.RegisterClientScriptBlock(this, this.GetType(), "scr", "Javascript:makeRequest(" + url + ");", true);
}
Posted
Comments
Richard Deeming 24-Oct-14 14:05pm    
As you're already loading jQuery, you might have better luck using jQuery's AJAX methods. For example:
jQuery.get[^]

1 solution

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[^]:
JavaScript
function makeRequest(url){
    $.get(url)
        .done(function(data){ alert(data); })
        .fail(function(){ alert('There was a problem with the request.'); });
}
 
Share this answer
 
Comments
Sergey Alexandrovich Kryukov 24-Oct-14 16:52pm    
Sure, 5ed.
—SA

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900