开发者

Looking for an extremely simple AJAX script

开发者 https://www.devze.com 2023-03-04 17:46 出处:网络
I need a very basic, simple and lightweight AJAX script. Does anyone have a shell of such a script they can share?

I need a very basic, simple and lightweight AJAX script.

Does anyone have a shell of such a script they can share?

Here's what I have:

  • I have a PHP script on the server that echo's the current date and time on the server
  • I just need the javascript that calls 开发者_如何学Pythonthe php script and loads the echoed text string into a js var so I can use it in my app

(the reason I need the server's clock is that all visitors to the site have to work off the same clock. The app does not work for visitors outside the server's timezone.)

Thanks for helping out.


JQuery is perhaps the right answer for AJAX but you can also do this in plain old Javascript as follows:

 <html>
<head>
    <script type="text/javascript">
        function loadXMLDoc(){
            var xmlhttp;
            if (window.XMLHttpRequest)  {
                // code for IE7+, Firefox, Chrome, Opera, Safari
                xmlhttp=new XMLHttpRequest();
            }
            else {// code for IE6, IE5
                xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
            }

            //the callback function to be callled when AJAX request comes back
            xmlhttp.onreadystatechange=function(){
                if (xmlhttp.readyState==4 && xmlhttp.status==200){
                    document.getElementById("myDiv").innerHTML=xmlhttp.responseText;
                }
            }       
            xmlhttp.open("POST","<<url of web address>>",true);
            xmlhttp.setRequestHeader("Content-type","application/x-www-form-urlencoded");
            xmlhttp.send("fname=Henry&lname=Ford");
    }
    </script>
</head>
<body>

<h2>AJAX</h2>
<button type="button" onclick="loadXMLDoc()">Request data</button>
<div id="myDiv"></div>

</body>
</html>


You can find a simple example here:

AjaxCall = function(Data, WebServiceURL, Callback) {
    var request;
    var url = WebServiceURL;

    if (window.XMLHttpRequest) {
        request = new XMLHttpRequest();
        request.onreadystatechange = function() {
            if (request.readyState === 4) {
                if (request.status === 200) {
                    Callback(request);
                } else {
                    alert("Sorry, an error occurred. " + request.responseText);
                }
            }
        };
        request.open("POST", url, true);
        request.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
        request.send(Data);
    } else if (window.ActiveXObject) {
        url += "?" + Data;
        request = new ActiveXObject("Microsoft.XMLHTTP");

        if (request) {
            request.onreadystatechange = function() {
                if (request.readyState === 4) {
                    if (request.status === 200) {
                        Callback(request);
                    } else {
                        alert("Sorry, an error occurred. " + request.responseText);
                    }
                }
            };
            request.open("GET", url, true);
            request.send();
        }
    }
};

The the ajax functionality in jQuery is great but does mean a greater page download for one simple Javascript function.

You can find a downloadable fully worked example on my blog here:

http://www.willporter.co.uk/blog/simple-ajax-script.aspx

It uses ASP.NET on the server side but you should get the idea.


jQuery has made very simple ajax methods for you to use. You can find more information about them here.

Sample:

 $.ajax({
     type: 'GET',
     url: '/SomeUrl/On/The/Server',
     data: { SomeValue: 10 },
     success: function(data, status)
     {
         // On Success
     },
     error: function(data, status)
     {
        // On Error
     }
 });


Look into this maybe : http://www.scriptiny.com/2011/01/simple-ajax-function-example/


jQuery is a more reliable library overall, but the lightest-weight AJAX methods I have found are the extremely simple Feather AJAX, coming in at 1.6 KB (with room for compression), or a one-liner snippet that I can't guarantee.

The risk of extremely lightweight libraries is that if they break, you're relying on the owner to fix it instead of a team of developers.


An alternative approach to solving your problem is to based your times on UTC instead of server-local time. You can even show the client local times based on that utc time, with a little work.


May I suggest AJAX Generator? I am developer, and it is commercial tool but it has demo as well.

What you could do with that tool is:

  • put annotation on your PHP function
  • run AJAX Generator on PHP source file
  • include generated JavaScript file in HTML page and use PHP service as if you were calling function

To make it more clear, here is example code:

//example.php
<?php
//@WebService
function getServerDate(){
    return date('m/d/Y h:i:s a', time());
}
?>

Now run generator on: example.php

Output would be two files: example_service.php and example_caller.js

Now you need to:

  • add example_service.php in same directory where is example.php and
  • include example_caller.js in index.html

Looking for an extremely simple AJAX script

Sorry for posting image instead of HTML code, but it wasn't showing properly.

0

精彩评论

暂无评论...
验证码 换一张
取 消