-1

I want to write a javascript-based front, which can get data from an external url and display them in the page. Is there any sample code? Thank you.

The server side has a python script, which can generate some data result. I need to get the data and display them. I may need jQuery to make a query to the server, but I don't know how to do it.

I would like to have a button, after I click the botton, it can generate the query which can get the data from the server and display it in the page.

To be brief, The front is javascript and the back is python. I need to make a query from the front to the back and run the python, then I can get some data. Then I need to display the data on the page.

  • possible duplicate of [How to get data with JavaScript from another server?](http://stackoverflow.com/questions/578095/how-to-get-data-with-javascript-from-another-server) – Thilo Jun 21 '12 at 00:42
  • 2
    Way, way too broad. Is there any specific part of this task you need help with? – millimoose Jun 21 '12 at 00:42
  • `window.location="http://stackoverflow.com";` – Managu Jun 21 '12 at 00:44
  • What research have you done? What have you already tried? Take a look at http://stackoverflow.com/questions/how-to-ask – Managu Jun 21 '12 at 01:18

1 Answers1

1

You might Google XMLHttpRequest. Wikipedia has a pretty good looking entry describing general technique, caveats, and restrictions.

Adapting from Wikipedia, you might start with this (assuming you're not using some ancient version of Internet Exploder):

<html>
<head></head>
<body>
    <script language="JavaScript">
        xmlhttp=new XMLHttpRequest();
        xmlhttp.onreadystatechange = function() {
                if(xmlhttp.readyState == 4){
                    alert(xmlhttp.responseText);                
                }
        };
        xmlhttp.open("GET","somepage.xml",true);
        xmlhttp.send(null);
    </script>
</body>
</html>

Alternately, common libraries such as jQuery can be used to simplify code making remote requests.

Managu
  • 8,849
  • 2
  • 30
  • 36
  • Hi Managu, the page is not a xml page, it is just a html. And the server is running python. I just need to make a query to the url and get the data back and display them. I know i need to use jQuery, but I just don't know how to use it... – user1470750 Jun 21 '12 at 01:02
  • @user1470750: The naming of `XMLHttpRequest` is unfortunate, a legacy of its birth. In practice, `XMLHttpRequest` is used to fetch text, JSON, XML, HTML, or just about any other data format you can imagine. – Managu Jun 21 '12 at 01:06
  • Is the data you're fetching from the server going to be a complete document, just a fragment of HTML, or something else entirely? Do you want the result to replace your current page, or do you just want to add something to your page? If you want to fetch something and add it to your current page, you're likely going to be working with `XMLHttpRequest`. Or using a library which wraps `XMLHttpRequest`. – Managu Jun 21 '12 at 01:07
  • If you're *certain* you need jQuery, perhaps you should modify your question to be "How do I do (this) with jQuery?" – Managu Jun 21 '12 at 01:09