locked
The "onreadystatechange" dosen't work, What I am doing wrong ?

    Question

  • I am have this wired problem, every time the  onreadystatechange called its not returning anything. for example I remove the if and I tell it to return yes that the state of the ajax change its still not returning instead I will get underfed any Idea why this is happening ?


    function load_page(php_handler, url ,searchString) { // Get the ajax object using our function above. window.ajax = makeAJAXObject(); // Tell the AJAX object what to do when it's loaded the page window.ajax.onreadystatechange = function () { if (window.ajax.readyState == 4) { // 4 means it's loaded ok. // For simplicity, I'll just return this

    return ("yes"); } } // Set up the variables you want to sent to your PHP page (namely, the URL of the page to load) var queryString = "?url=" + url; // Load the PHP script that opens the page window.ajax.open("GET", php_handler + queryString, true); window.ajax.send(null); }


    here is my makeAJAXObject function 

     function makeAJAXObject() { // seems to be working ok 
                var ajaxRequest;  // The variable that makes Ajax possible!
    
                try {
                    // Opera 8.0+, Firefox, Safari
                    ajaxRequest = new XMLHttpRequest();
                } catch (e) {
                    // Internet Explorer Browsers
                    try {
                        ajaxRequest = new ActiveXObject("Msxml2.XMLHTTP");
                    } catch (e) {
                        try {
                            ajaxRequest = new ActiveXObject("Microsoft.XMLHTTP");
                        } catch (e) {
                            // Something went wrong
                            return false;
                        }
                    }
                }
                return ajaxRequest;
            }

    I also tried to copy a working version of the script from example I found here and the same thing happens, I am working on the split form that coming with the studio.

    Please help !

    Friday, May 31, 2013 5:20 PM

Answers

  • Hi,

    Why don't you use the WinJS native xhr? (see the link for full documentation)

    WinJS.xhr({ url: "http://your_url" }).done(function completed(result){
     if (result.status === 200) {
    //do what you need with the result
     }
    },
    function error(result){
    //handle error
    });

    • Marked as answer by Daniel Iliya Friday, June 07, 2013 3:52 PM
    Monday, June 03, 2013 2:07 PM

All replies

  • that seems like a lot of code for a simple task. try this...

    serverxmlhttp = new XMLHttpRequest();
    
        serverxmlhttp.open("GET", "http://www.website.com/api.php?id=" + Math.random(), true);
        serverxmlhttp.onreadystatechange = function (oEvent) {
            if (serverxmlhttp.readyState === 4) {
                if (serverxmlhttp.status === 200) {
                   console.log(serverxmlhttp.responseText);
                }
            }
        };
    
    serverxmlhttp.send();

    I always throw in a dummy chunk as to never use a cache when GET or POST is used, that's why you see the id=Math.random();. you can eliminate that if you want. This should work on your machine, just make sure you have internet under you app manifest selected.
    Monday, June 03, 2013 7:08 AM
  • Hi,

    Why don't you use the WinJS native xhr? (see the link for full documentation)

    WinJS.xhr({ url: "http://your_url" }).done(function completed(result){
     if (result.status === 200) {
    //do what you need with the result
     }
    },
    function error(result){
    //handle error
    });

    • Marked as answer by Daniel Iliya Friday, June 07, 2013 3:52 PM
    Monday, June 03, 2013 2:07 PM