Click here to Skip to main content
15,887,822 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Hi Everyone,

I have a question that triggers me a lot to fix on developing a JSON QUERY. My question is how can I post an AJAX code in which it make an automatic load without clicking the button to load the data from the service I created as follows a code:


JavaScript
function testJson() {
        $.ajax({
            type: "POST",
            url: "http://112.206.229.182:91/JSON/Service.asmx/GetLatestText",
            data: JSON.stringify({passKey: "A4CA714AEBFA482A96D9091BABB4C51B", textID: 0}),
            contentType: "application/json; charset=utf-8",
            dataType: "json",
            success: function (msg) {
                try{
                    $("#jsonResponse").html(msg.d);
                    var dTbl = eval(msg.d);

                    var t = "<table border=1><tr>" +
                      "<td><strong>Output ID</strong></td>" +
                      "<td><strong>Output Msg</strong></td>" +
                      "<td><strong>ID</strong></td>" +
                      "<td><strong>HomeEventsText</strong></td>" +
                      "<td><strong>FontColor</strong></td>" +
                      "<td><strong>FontFamily</strong></td>" +
                      "<td><strong>FontSize</strong></td></tr>";

                    for (var i in dTbl) {
                        t = t + "<tr>" +
                            "<td> " + dTbl[i][0] + "</td>" +
                            "<td> " + dTbl[i][1] + "</td>" +
                            "<td> " + dTbl[i][2] + "</td>" +
                            "<td> " + dTbl[i][3] + "</td>" +
                            "<td> " + dTbl[i][4] + "</td>" +
                            "<td> " + dTbl[i][5] + "</td>" +
                            "<td> " + dTbl[i][6] + "</td>" +
                            "</tr> ";
                    }

                    t = t + " </table> ";

                    $("#jsonDiv").html(t);
                }
                catch (err) {
                    $("#jsonDiv").html(err.message);
                }
            },
            error: function (msg) {
                $("#jsonDiv").html("ERROR - " + msg);
            },
            failure: function(msg) {
                $("#jsonDiv").html("FAILED");
            }
        });
    };

</script>

HTML
</head>
<body>
    <p>
        <input id="testjson" type="button" value="Test JSON Call" onclick="testJson()" />
    </p>
    <br />
    <strong>Message Response</strong>
    <br />
    <div id="jsonResponse" style="display:block;"></div>
    <br />
    <strong>Processed Result</strong>
    <br />
    <div id="jsonDiv" style="display:block;"></div>
</body>
</html>
Posted
Updated 31-Jul-14 23:27pm
v2

Put your function testJson() in jQuery document.ready function

JavaScript
$(document).ready(function(){
   testJson();
});



or you can set that function onload of body

HTML
<body onload="testJson()">

</body>
 
Share this answer
 
The answer is simple, since you're handling this script on the onclick event it would always trigger that way. If you want it to happen as soon as the page loads you can try to add it to the body tag, like this

HTML
<body onload="testJson()"></body>


This would execute the very time when you will run the page to load it.

One more thing as Nirav has also mentioned would be to make sure the jQuery code is present inside the $(document).ready(function () { ... }); block. But I don't think that is a problem around since the code works in the click event.
 
Share this answer
 
Thanks For the response Guys

Its Already Working.
 
Share this answer
 

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