Obtain data from the sample page using the jQuery AJAX method

advertisements

I am given a sample page to obtain a string from it. The I am to convert this sting to a variable that can be used for a different code. I been trying for hours to get the data from the page but I am at a lost.

First I created a sample code to read the page and using the Chrome console I could see the word I needed to get. This is the sample code

<html>
<head>
<script src="http://code.jquery.com/jquery-1.10.1.min.js">
</script>
<script>
$(document).ready(function(){
    $("#loaddata").click(function(){
        $.post("http://www.assessmentnj.com/Services/MagicWord.asmx/GetMagicWord",function(data){
        console.log(data);
        datafromwebsite = data;
        magicWordResult = "Your word to guess is " + datafromwebsite;
        alert(magicWordResult);
        });
    });
});
</script>
</head>
<body>
    <div>Get the object from sample page:</div>
    <button id="loaddata">Load data</button>
    </body>
</html>

Now I created a second code to get the string and create my variable, but I am not sure how to do this. I researched books and websites and I am not sure. Any suggestions? Thank you guys. Here is the code I am using:

<html>
<head>
<script src="http://code.jquery.com/jquery-1.10.1.min.js">
</script>
<script>
$(document).ready(function(){
    $("#loaddata").click(function(){
        $.ajax({
            type: "POST",
            url: "http://www.assessmentnj.com/Services/MagicWord.asmx/GetMagicWord",
            dataType: "json",
            success:function(data){
                console.log(data);
            }
        });
    });
});
</script>
</head>
<body>
    <div>Get the object from sample page:</div>
    <button id="loaddata">Load data</button>
    </body>
</html>


Instead of

$.ajax({
    type: "POST",
    url: "http://www.assessmentnj.com/Services/MagicWord.asmx/GetMagicWord",
    dataType: "json",
    success:function(data){
        console.log(data);
    }
});

try

$.get('http://www.assessmentnj.com/Services/MagicWord.asmx/GetMagicWord', function(xml) {
    // this should contain your magic word
    console.log(xml.childNodes[0].innerHTML);
}, 'xml');