4

这是我第一次使用 Codeigniter。很抱歉,如果我的问题是菜鸟。我从这个站点获得了代码:Working with IDE One API (Full project code is here ),现在,我正在尝试在 codeigniter 中使用它。

我能够使其工作到下面的 javascript 部分(因为它能够检查“源”是否具有价值)。

脚本.js:

$(document).ready(function() {
$('#myform').submit( function(){
    var data = $(this).serialize();
    var source = $('textarea#source').val();

    if( source == '' ) {
        alert( 'No source code provided');
        return false;
    }

    $(this).append('<div class="loading">Processing...</div>');

    $.ajax({
        type: "POST",
        url: "<?php echo base_url();?>student/submit",
        dataType: 'json',
        data: data + '&process=1',
        cache: false,
        success: function(response){
            $('.loading').remove();
            $('.cmpinfo').remove();
            $('#response').show();
            //alert(response);
            console.log(response.raw);
            if( response.status == 'success' ) {
                $('.meta').text( response.meta );
                $('.output').html('<strong>Output</strong>: <br><br><pre>' + response.output + '</pre>');

                if( response.cmpinfo ) {
                    $('.cmpinfo').remove();
                    $('.meta').after('<div class="cmpinfo"></div>');
                    $('.cmpinfo').html('<strong>Compiler Info: </strong> <br><br>' + response.cmpinfo );
                }

            } else {
                //$('.output').html('<pre>' + response + '</pre>');
                alert( response.output );
            }
            //alert( response.msg );
        }
    });

    return false;
});
});

我的问题是我不确定它是否能够在我的学生中实现“提交”功能

控制器或问题在于功能本身。

学生/提交

    public function submit(){
    $user = 'myuser';
    $pass = 'mypassword';
    $code = '';
    $input = '';
    $run = true;
    $private = false;

    $subStatus = array(
        0 => 'Success',
        1 => 'Compiled',
        3 => 'Running',
        11 => 'Compilation Error',
        12 => 'Runtime Error',
        13 => 'Timelimit exceeded',
        15 => 'Success',
        17 => 'memory limit exceeded',
        19 => 'illegal system call',
        20 => 'internal error'
    );

    $error = array(
        'status' => 'error',
        'output' => 'Something went wrong :('
    );

    //echo json_encode( array( 'hi', 1 ) ); exit;
    //print_r( $_POST ); exit;

    if ( isset( $_POST['process'] ) && $_POST['process'] == 1 ) {
        $lang = isset( $_POST['lang'] ) ? intval( $_POST['lang'] ) : 1;
        $input = trim( $_POST['input'] );
        $code = trim( $_POST['source'] );

        $client = new SoapClient( "http://ideone.com/api/1/service.wsdl" );

        //create new submission
        $result = $client->createSubmission( $user, $pass, $code, $lang, $input, $run, $private );

        //if submission is OK, get the status
        if ( $result['error'] == 'OK' ) {
            $status = $client->getSubmissionStatus( $user, $pass, $result['link'] );
            if ( $status['error'] == 'OK' ) {

                //check if the status is 0, otherwise getSubmissionStatus again
                while ( $status['status'] != 0 ) {
                    sleep( 3 ); //sleep 3 seconds
                    $status = $client->getSubmissionStatus( $user, $pass, $result['link'] );
                }

                //finally get the submission results
                $details = $client->getSubmissionDetails( $user, $pass, $result['link'], true, true, true, true, true );
                if ( $details['error'] == 'OK' ) {
                    //print_r( $details );
                    if ( $details['status'] < 0 ) {
                        $status = 'waiting for compilation';
                    } else {
                        $status = $subStatus[$details['status']];
                    }

                    $data = array(
                        'status' => 'success',
                        'meta' => "Status: $status | Memory: {$details['memory']} | Returned value: {$details['status']} | Time: {$details['time']}s",
                        'output' => htmlspecialchars( $details['output'] ),
                        'raw' => $details
                    );

                    if( $details['cmpinfo'] ) {
                        $data['cmpinfo'] = $details['cmpinfo'];
                    }

                    echo json_encode( $data );
                } else {
                    //we got some error :(
                    //print_r( $details );
                    echo json_encode( $error );
                }
            } else {
                //we got some error :(
                //print_r( $status );
                echo json_encode( $error );
            }
        } else {
            //we got some error :(
            //print_r( $result );
            echo json_encode( $error );
        }
    }

    }

视图部分:

<div id="wrapper">
        <h1><a href="index.php">Online Compiler</a></h1>
        <?php echo form_open("student/submit",array('id' => 'myform'));  ?>
            <div>
            <label for="lang">Select Language:</label>
            <select name="lang" id="lang">
                <option value="7    ">Ada (gnat-4.3.2)</option>
                <option value="13">Assembler (nasm-2.07)</option>
                <option value="45">Assembler (gcc-4.3.4)</option>
                <option value="104">AWK (gawk) (gawk-3.1.6)</option>
                <option value="105">AWK (mawk) (mawk-1.3.3)</option>
                <option value="28">Bash (bash 4.0.35)</option>
                <option value="110">bc (bc-1.06.95)</option>
                <option value="12">Brainf**k (bff-1.0.3.1)</option>
                <option value="11">C (gcc-4.3.4)</option>
                <option value="27">C# (mono-2.8)</option>
                <option value="1" selected="selected">C++ (gcc-4.3.4)</option>
                <option value="44">C++0x (gcc-4.5.1)</option>
                <option value="34">C99 strict (gcc-4.3.4)</option>
                <option value="14">CLIPS (clips 6.24)</option>
                <option value="111">Clojure (clojure 1.1.0)</option>
                <option value="118">COBOL (open-cobol-1.0)</option>
                <option value="106">COBOL 85 (tinycobol-0.65.9)</option>
                <option value="32">Common Lisp (clisp) (clisp 2.47)</option>
                <option value="102">D (dmd) (dmd-2.042)</option>
                <option value="36">Erlang (erl-5.7.3)</option>
                <option value="124">F# (fsharp-2.0.0)</option>
                <option value="123">Factor (factor-0.93)</option>
                <option value="125">Falcon (falcon-0.9.6.6)</option>
                <option value="107">Forth (gforth-0.7.0)</option>
                <option value="5">Fortran (gfortran-4.3.4)</option>
                <option value="114">Go (gc-2010-07-14)</option>
                <option value="121">Groovy (groovy-1.7)</option>
                <option value="21">Haskell (ghc-6.8.2)</option>
                <option value="16">Icon (iconc 9.4.3)</option>
                <option value="9">Intercal (c-intercal 28.0-r1)</option>
                <option value="10">Java (sun-jdk-1.6.0.17)</option>
                <option value="35">JavaScript (rhino) (rhino-1.6.5)</option>
                <option value="112">JavaScript (spidermonkey) (spidermonkey-1.7)</option>
                <option value="26">Lua (luac 5.1.4)</option>
                <option value="30">Nemerle (ncc 0.9.3)</option>
                <option value="25">Nice (nicec 0.9.6)</option>
                <option value="122">Nimrod (nimrod-0.8.8)</option>
                <option value="43">Objective-C (gcc-4.5.1)</option>
                <option value="8">Ocaml (ocamlopt 3.10.2)</option>
                <option value="119">Oz (mozart-1.4.0)</option>
                <option value="22">Pascal (fpc) (fpc 2.2.0)</option>
                <option value="2">Pascal (gpc) (gpc 20070904)</option>
                <option value="3">Perl (perl 5.12.1)</option>
                <option value="54">Perl 6 (rakudo-2010.08)</option>
                <option value="29">PHP (php 5.2.11)</option>
                <option value="19">Pike (pike 7.6.86)</option>
                <option value="108">Prolog (gnu) (gprolog-1.3.1)</option>
                <option value="15">Prolog (swi) (swipl 5.6.64)</option>
                <option value="4">Python (python 2.6.4)</option>
                <option value="116">Python 3 (python-3.1.2)</option>
                <option value="117">R (R-2.11.1)</option>
                <option value="17">Ruby (ruby-1.9.2)</option>
                <option value="39">Scala (scala-2.8.0.final)</option>
                <option value="33">Scheme (guile) (guile 1.8.5)</option>
                <option value="23">Smalltalk (gst 3.1)</option>
                <option value="40">SQL (sqlite3-3.7.3)</option>
                <option value="38">Tcl (tclsh 8.5.7)</option>
                <option value="62">Text (text 6.10)</option>
                <option value="115">Unlambda (unlambda-2.0.0)</option>
                <option value="101">Visual Basic .NET (mono-2.4.2.3)</option>
                <option value="6">Whitespace (wspace 0.3)</option>
            </select>
        </div>

        <div>
            <label for="source">Source Code:</label>
            <textarea cols="40" rows="10" name="source" id="source"></textarea>
        </div>

        <div >
            <label for="input">Input: <span class="description">(Data that will be given to the program on the stdin.)</span></label>
            <textarea cols="40" rows="3" name="input" id="input"></textarea>
        </div>

        <div>
            <input type="submit" name="submit" value="Submit" />
        </div>
   <?php echo form_close(); ?>

    <div id="response">
        <div class="meta"></div>
        <div class="output"></div>
    </div>


</div>

它只是停留在“处理”显示的部分,之后什么也没有发生。请帮我。非常感谢 :(

我不确定这是否有助于解决问题。但是当我跟踪网络请求时,我得到“不允许的关键字符”作为“响应”。我认为这是因为请求 URL:

//localhost/ci_user/index.php/student/%3C?php%20echo%20base_url();%20?%3Estudent/submit

我有进展吗?请帮忙

4

1 回答 1

1

我得到了它!感谢一位教我关于 Chrome 的“CTR+SHIFT+I”的朋友。请求 URL确实是错误。

我只是在脚本的 AJAX 发布请求中更改了这一部分:

 url: "<?php echo base_url();?>student/submit",

    url: "submit",

CI 发送的响应/错误是“Disallowed Key Characters”,这是因为过多的 php 代码与请求 URL(带有 base_url() 的那个)混淆了。

多亏了这个问题(无论这多么简单),我能够对 codeigniter 变通方法进行非常好的介绍。

于 2013-09-22T22:30:49.813 回答