Question

So, TideSDK say that php is preprocessed upon each request (if it's a .php file).

I'm using the following JS ajax:

function ajax(url, method, data, async)
{
method = typeof method !== 'undefined' ? method : 'GET';
async = typeof async !== 'undefined' ? async : false;

if (window.XMLHttpRequest)
{
    var xhReq = new XMLHttpRequest();
}
else
{
    var xhReq = new ActiveXObject("Microsoft.XMLHTTP");
}


if (method == 'POST')
{
    xhReq.open(method, url, async);
    xhReq.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
    xhReq.setRequestHeader("X-Requested-With", "XMLHttpRequest");
    xhReq.send(data);
}
else
{
    if(typeof data !== 'undefined' && data !== null)
    {
        url = url+'?'+data;
    }
    xhReq.open(method, url, async);
    xhReq.setRequestHeader("X-Requested-With", "XMLHttpRequest");
    xhReq.send(null);
}

return xhReq.responseText;
console.log("[ajax] Request Completed.");
}

and my index.php file is:

<?php echo "Test"; ?>

ajax is called as such

console.log(ajax('index.php', 'GET'));

Instead of returning 'Test' it just returns the source code.

Am I doing something wrong, or is this expected. Other-wise, what could I do too get the expected output: the pre processed PHP.

Was it helpful?

Solution

If you want to do a ajax get request on your php script, use the jQuery ajax method.

RTM: http://api.jquery.com/jquery.ajax/

Example GET Request:

$(document).ready(function()
{
    $.get("index.php", function(data) {
        console.log(data);
    });
});

Example POST Request:

$(document).ready(function()
{
    $.ajax({
        type: "POST",
        url: "index.php",
        data: { "test": "hello world", "anotherkey": "andvalue" },
        success: function(data) {
            console.log(data);
        }
    });
});

// usage in php: $post_test = $_POST['test']; // returns 'hello world'
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top