Click here to Skip to main content
15,888,454 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
JavaScript
var url="http://api.ipinfodb.com/v3/ip-city/?key=9a9791a9d6079663d802274e09dcb8170bf98fdfaa0eb37da604d1d83d5deb56&ip=72.32.84.70";
        var response = null;
        var connection = new XMLHttpRequest();
        try
        {
            connection.open("GET", url, false);
           connection.send();
            if(connection.readyState == 4&& connection.status==200) response = connection.responseText;
        }
        catch(e)
        {
           alert("ERROR: The remote host could not be found or the connection was refused.");
            return false;
        }
        alert(response);


Important thing url is having text value (my need) , I watched the url (page source) not having html or body or any element. But having the text.

I tried innerhtml,body,values but all displays empty.

My thought is may be that url is casted or embeded with a text document or word document. But i need that value in a javascript variable.

Please if anybody crossed this help me.
Posted
Updated 16-Dec-11 6:30am
v7
Comments
Sergey Alexandrovich Kryukov 16-Dec-11 12:14pm    
Do you need URL or the whole resource server by the server on this URL? In this case you need to load it.
--SA
vino2012 16-Dec-11 12:25pm    
hitting the url gives a value in that page. I have to use that value for my page.
Thing is the value is not bounded by any tag.........
Sergey Alexandrovich Kryukov 16-Dec-11 13:10pm    
I think I understand what you need. Please see my solution. Even if you needs something else, it will give you the right idea.
--SA

Here is the problem: you can do it of course, but asynchronously. That said, when you start loading the iframe source page by Ajax, it won't block execution of the script, so further execution might override the result of your callback.

I prefer doing it with jQuery. Consider this:

XML
<html>
<head>
    <title></title>
    <script type="text/javascript" src="scripts/jquery-1.7.1.min.js"></script>
</head>
<body>

    <!-- let's assume test.html contains the string 'Ajax does work!' -->
    <iframe id="myIframe" src="test.html"></iframe>

    <script type="text/javascript">

        $(document).ready(function() {
            var content;
            var iFrameSource = $("#myIframe").attr('src') //will assign "test.html"
            $.get(iFrameSource, function(data) {
                content = data; //will not be assigned immediately after the call to get
                alert('Load was performed: ' + data);
                document.write(content);
            });
        }); </script>
        // at this moment, content is still not assigned

</body>
</html>


If you try to use variable content, it will be still undefined. So, instead of attempting to write a function which would return the content, try to do all you need in the callback. Better yet, try to use the function load to load your content onto some HTML element:

XML
<html>
<head>
    <title></title>
    <script type="text/javascript" src="scripts/jquery-1.7.1.min.js"></script>
</head>
<body>

    <!-- let's assume test.html contains the string 'Ajax does work!' -->
    <iframe id="myIframe" src="test.html"></iframe>
    <h2 id="sink">Initial value</h2>

    <script type="text/javascript">

        $(document).ready(function() {
            var content;
            var iFrameSource = $("#myIframe").attr('src')
            $("#sink").load(iFrameSource)
        }); </script>

</body>
</html>


If you need to do some calculation on the loaded content, do it in a callback when a resource is loaded. Don't forget asynchronous nature of Ajax.

—SA
 
Share this answer
 
v3
Comments
vino2012 16-Dec-11 16:25pm    
Dear friend
Thanks for ur kind help. I already tried on jquery and ajax. Still,I am not able to get. Your 1st solution working on IE browser when i tested, after hosting my files on live its not working.
So,finally went to server side code .net and i got output.

But I understood your samples and it suits for retrieving data which enclosed in an element inside web page . Nice and I am going to use for my further task.I posted my server side, refer solution 2 above. Feel happy to share with u...
Thanks
string strURL;
     string strResult;

     strURL = "http://url......";

     WebRequest request = WebRequest.Create(strURL);

     WebResponse response;

     request.Method = "GET";// Supports POST too

     response = request.GetResponse();
     StreamReader sr = new StreamReader(response.GetResponseStream());
     strResult = sr.ReadToEnd();       
     sr.Close();

     TextBox1.Text = strResult;


// c#-asp.net// so finally i got data from a web page and the data is not enclosed by any element on that page
 
Share this answer
 
v2

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