-1

I have the following javascript code:

$(document).ready(function()
{
    alert("test");
    ajaxrequest();
});
function ajaxrequest()
{
    alert("test");
    $.ajax({
        url:"/CRUD_LOGIN/php/getUserRank.php",
        type: "GET",
        dataType: 'json',
        success:function(result){
            alert(result);
            if(result.rank == "A")
            {
                $("#bar").append("<li><a href='/CRUD_LOGIN/page/display.php'>Admin panel</a></li>");
            }
        }
   });
}

but it isnt executed, and even the alerts are ignored... by the way, the getUserRank.php does this:

echo json_encode(array("rank" => "A"));
gab
  • 1
  • 1
    Have you checked your console for errors? Did you include the jQuery library? If so did you put it before this code? – Patrick Evans Nov 10 '17 at 14:58
  • Well, if you strip out the AJAX [the alerts certainly work](https://jsfiddle.net/gjop8g8v/), so something else is going on. – Andy Nov 10 '17 at 15:01
  • this js file is included in my profile.php file, which contains header.html, which calls bootstrap and jquery libraries... so I think I did, hopefully that's not the problem – gab Nov 10 '17 at 15:01
  • you should add an error function instead of having only a success. A lot of people do this, but even in production code, this can be helpful.. – Kaddath Nov 10 '17 at 15:05
  • thanks for the tip Kaddath, will do it right now while I wait for an answer: do you know why, even if this script is called in the file profile.php, where jquery is also included, it still doesnt work? – gab Nov 10 '17 at 15:07
  • As it is, i don't see why.. that's why for the error function, it can help find the reason. – Kaddath Nov 10 '17 at 15:14
  • Since you get `$ is not defined` check out [this question](https://stackoverflow.com/questions/2194992/jquery-is-not-defined) – James Nov 10 '17 at 15:16
  • solved, apparently the problem was that i was calling this script before including jquery in the profile.php file. Thanks! – gab Nov 10 '17 at 15:24

1 Answers1

0

If you have a line like this in console: $ is not defined you haven't included jQuery or not above $(document).ready(function() script

Shiladitya
  • 12,003
  • 15
  • 25
  • 38
oxmolol
  • 125
  • 1
  • 1
  • 10
  • I actually have that line in my console, but as I wrote in the other comment: "this js file is included in my profile.php file, which contains header.html, which calls bootstrap and jquery libraries... so I think I did, hopefully that's not the problem", so I think it's not enough? – gab Nov 10 '17 at 15:04
  • include jquery lib in the to avoid this problem, not in your profile.php – oxmolol Nov 10 '17 at 15:22