Using JQuery AJAX to call PHP script

Discussion in 'PHP' started by DEADFA11, Jul 6, 2010.

  1. #1
    Hello there. I am planning to use a JQuery AJAX call to a PHP script that accesses a REST servlet. My php script would be very simple; something like:
    
    <?php
        // call the RESTlet to grab data from the database
        $output = file_get_contents('http://localhost:xxxx');
        echo $output;
    ?>
    Code (markup):
    This is the code for my AJAX call:
    
    $.ajax({
                            url: 'myscript.php',
                            dataType: 'json',
                            success: function(series)
                            {
                                // plot the data with Flot
                                plot = $.plot(placeholder, [series], options);
                            }
                        });
    Code (markup):
    This is the error that Firefox's Error Console gives me:

    "Error: no element found
    Source File: file:///C:/workspace/Database/myscript.php
    Line: 9, Column: 3
    Source Code:
    ?>"
    Code (markup):
    I'm an experienced programmer, but I'm fairly new to web development, so I am having trouble interpreting this error. What have I done wrong? Is my PHP script well-formed?
     
    DEADFA11, Jul 6, 2010 IP
  2. AsHinE

    AsHinE Well-Known Member

    Messages:
    240
    Likes Received:
    8
    Best Answers:
    1
    Trophy Points:
    138
    #2
    Are you running your script under webserver, cause
    does not look like you are.
    What does your ajax script return? You can see it with firebug or fiddler.
    Providing this information will help us to help you.
     
    AsHinE, Jul 6, 2010 IP
  3. DEADFA11

    DEADFA11 Peon

    Messages:
    2
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #3
    I don't know how to run a PHP script under a web server. I've downloaded PHP from http://windows.php.net/download/ and when I run the script using php.exe it seems to run correctly. For a brief moment before the CLI window closes, I can see some of the data it was supposed to return.

    However, I don't know how to get my AJAX call to run the script using php.exe. I tried telling it to use 'php.exe myscript.php' as the url with php.exe in the same directory, to try to run the script CLI-argument style, but no dice.

    If I have to run a web server to execute a 2-line script like that, I'll be a little disappointed.

    As for the return of the AJAX script, I'll have to check at home. The administrator types at work don't give us sufficient privileges to install such things as Firebug. If I can get a hold of a machine where I can test that, I'll report back.
     
    DEADFA11, Jul 6, 2010 IP
  4. AsHinE

    AsHinE Well-Known Member

    Messages:
    240
    Likes Received:
    8
    Best Answers:
    1
    Trophy Points:
    138
    #4
    This is the main problem, but I don't sure if it is possible.

    One solution I see is pregenerating files with data from REST service and loading them - so you don't need to execute php script, but I don't know if it suitable for you.
    If you don't want to configure webserver manually you can try preconfigured packages like http://www.apachefriends.org/en/xampp.html
     
    AsHinE, Jul 6, 2010 IP