0

I've been learning to use AJAX with the GET request that allows me to access a PHP script with an array of data on a server. I want to be able to send a request that tells the server to run code that will open an application and manipulate some info on this application.

Here is the code I use to firstly communicate with the server, then send a request to the server and finally handle responses from the server.

var xmlHttp = createXmlHttpRequestObject();

function createXmlHttpRequestObject(){
    var xmlHttp;

    if(window.ActiveXObject)
    {
        try
        {
            xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
        }
        catch(e)
        {
            xmlHttp = false;
        }
    }
    else
    {
        try
        {
            xmlHttp = new XMLHttpRequest();
        }
        catch(e)
        {
            xmlHttp = false;
        }
    }

    if(!xmlHttp)
    {
        alert("cant create that object hoss");
    }
    else
    {
        return xmlHttp;
    }
}

function process(){
    if(xmlHttp.readyState == 0 || xmlHttp.readyState == 4) //State were object is free and ready to communicate with server
    {
        food = 'bacon';
        xmlHttp.open("GET", "ExecuteMaya.php?food="+food,true); //Creates request that we are sending to server
        xmlHttp.onreadystatechange = handleServerResponse;
        xmlHttp.send(null);
    }
    else
    {
        setTimeout('process()', 1000);
    }
}

function handleServerResponse(){
    if(xmlHttp.readyState == 4)
    {
        if(xmlHttp.status == 200) //Means communication was successful
        {
            var xmlResponse = xmlHttp.responseText;
            var xmldom = (new DOMParser()).parseFromString(xmlResponse, 'text/xml');
            var text = xmldom.getElementsByTagName("response")[0];
            var message = text.childNodes[0].nodeValue;
            foodTextOutput = message;
            setTimeout('process()', 1000);
        }
        else
        {
            alert('Something went wrong!');
        }
    }
}

Here is the PHP I was using while I was learning how to use AJAX. I got the following error when I printed the 'xmldom' variable from the above code to the console and inspected it - "error on line 2 at column 1: Extra content at the end of the document". This may be a different question to my original post, but I thought I'd bring up that this error occurred. This then had a knock on effect for the line 'var message = text.childNodes[0].nodeValue;' which produced the error - "Uncaught TypeError: Cannot read property 'childNodes' of undefined".

<?php
header('Content-Type: text/xml');
echo '<?xml version="1.0" encoding="UTF-8" standalone="yes" ?>':

echo '<response>';
    $food = $_GET['food'];
    $foodArray = array('tuna','bacon','beef','loaf','ham');
    if(in_array($food, $foodArray))
        echo 'We do have '.$food.'!';
    elseif($food == '')
        echo 'Enter a food you idiot';
    else
        echo 'Sorry punk we dont sell no '.$food.'!';
echo '</response>';
?>

The code that I have been working with to learn AJAX may not be relevant, I just thought I'd post it in case I can use some of this code that has already been written.

To sum up, I want to be able to do be able to send a boolean, or whatever is viable with AJAX, to the server that tells it to run a script. This script will then open a Maya application and run some Python code that I have written.

Thank you in advance!

skelto
  • 157
  • 1
  • 11
  • 1
    so - whats the question, where's the problem? – Jeff Jun 14 '16 at 13:34
  • @Jeff I'm not sure how to communicate with the server in such a way that I can tell it to run code. Also if someone could tell me why the errors I talked about were occurring. Thanks. – skelto Jun 14 '16 at 13:38
  • 1
    as soon as you call a php script via ajax (like you already did) it runs code! – Jeff Jun 14 '16 at 13:42
  • @Jeff so I would then be able to write code in the PHP script that would open a Maya application and run my Python code? – skelto Jun 14 '16 at 13:44

2 Answers2

1

As soon as you call the PHP file, this begins running code on the server. If you want to run an external application from PHP, take a look at the exec() function:

http://php.net/manual/en/function.exec.php

Dan
  • 1,209
  • 3
  • 13
  • 29
0

You have jQuery listed in your question tags. Have you compared the javascript and jQuery code?

The advantages of using jQuery are:

  • less typing,
  • simpler structure
  • automatically cross-browser
  • easily use Promises interface

Have a look at these examples and see if you prefer the jQuery AJAX methodologies:

Three simple examples

dynamic drop down box?

Chain AJAX Requests with jQuery Deferred

Community
  • 1
  • 1
cssyphus
  • 37,875
  • 18
  • 96
  • 111