2

EDIT: fixed _ typo (2x), added header, still logging 100.

Upon clicking a button in my JavaScript, I'm firing this function (parameter: 100)

ajaxManager = new AjaxManager();
ajaxManager.requestHexContent(100);

function AjaxManager (){
    this.requestHexContent = function(id){
        $.ajax({
            type : 'get',
            url : 'simulator/hexFiller.php',
            dataType : 'json',
            data: {
                gameid: id,
            },
            success : function(ret){
                console.log(ret);
            },
            error : function(){
                alert("error")
            }
        });
    },
}

this is my hexFiller.php

<?php   
header('Content-Type: application/json');

  $ret; 

  if (isset($_GET["gameid"])){
    if ($_GET["gameid"] == 100){
        $ret = 200;
    }
   else {
    $ret = "error";
 }
}

echo json_encode($ret);

?>

Now, what i would expect to happen is for my browser to log "200" to the console, or, "error". Instead it logs "100" to the console.

Can someone explain to me the fundamental error in my thinking?

7
  • How are you calling the function? Can we have the code when you fire this function upon clicking? Commented Oct 15, 2015 at 17:18
  • edited the code, also added in the call of requestHexContent() Commented Oct 15, 2015 at 17:21
  • 1
    Its working perfectly fine here. Earlier it was coming under error and alerted "error". When I changed $GET into $_GET. It returned 200. Anything else you wanted? Shall I paste the code? Commented Oct 15, 2015 at 17:28
  • mh. Perhaps something different is wrong with secureWAMP then. thanks for investigating. Commented Oct 15, 2015 at 17:29
  • 1
    as with @AshishChoudhary , my test logged 200 Commented Oct 15, 2015 at 18:47

4 Answers 4

1

As discussed in the comment, working code is mentioned below: I only replaced $GET with $_GET.

<script src="//ajax.googleapis.com/ajax/libs/jquery/1.11.2/jquery.min.js"></script>
<script>
    function AjaxManager() {
        this.requestHexContent = function (id) {
            $.ajax({
                type: 'get',
                url: 'simulator/hexFiller.php',
                dataType: 'json',
                data: {
                    gameid: id,
                },
                success: function (ret) {
                    console.log(ret);
                },
                error: function () {
                    alert("error")
                }
            });
        }
    }

    ajaxManager = new AjaxManager();
    ajaxManager.requestHexContent(100);
</script>

hexFiller.php

<?php
$ret;

if (isset($_GET["gameid"])) {
    if ($_GET["gameid"] == 100) {
        $ret = 200;
    } else {
        $ret = "error";
    }
}

echo json_encode($ret);
?>
Sign up to request clarification or add additional context in comments.

Comments

0

You have a slight error in your PHP code. You're trying to use $GET[] to get parameters passed to the PHP script. But, it's $_GET[] that you need to use. See http://php.net/manual/en/reserved.variables.get.php.

1 Comment

But why does it log 100?
0

You are not returning a json element from your PHP file.

Add header('Content-Type: application/json'); at the top of your PHP file.

Also your $_GET declaration is incorrect.

Comments

0

Why it is returning 100, I don't really know, probably an error number. But your PHP code is wrong, it's $_GET not $GET.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.