General developer forum

ajax request not work in moodle

 
Picture of sara rabie
ajax request not work in moodle
 

i try to make request by ajax to this page contain this code but it doesnt work by ajax it always return  guest but when run url without ajax it return username and id and it work on localhost but not work ajax result well on server any help please??????

<?php
header('Access-Control-Allow-Origin: *');
require_once('../../config.php');
global $USER, $CFG,$DB;
if(isset($USER) && isset($USER->username)){
    $user = ['name'=>$USER->username ,'id'=>$USER->id];
    echo json_encode($user);
    exit;
}else{
    $user = ['name'=>'guest'];
    echo json_encode($user);
    exit;
}

 
Average of ratings: -
Picture of Darko Miletić
Re: ajax request not work in moodle
Core developersParticularly helpful Moodlers

I would change your code to look like this:

define('AJAX_SCRIPT', true);
define('NO_DEBUG_DISPLAY', true);

require_once("/path/to/config.php");

header('Access-Control-Allow-Origin: *');

$response = ['name' => 'guest'];
if(isloggedin()) {
    $response = ['name' => $USER->username, 'id' => $USER->id];
}

echo json_encode($response);


As for why it does not work, that depends. You will have to post more code.


 
Average of ratings: -
Picture of Darko Miletić
Re: ajax request not work in moodle
Core developersParticularly helpful Moodlers

A bit more cleaned version that follows general Moodle guidelines

define('AJAX_SCRIPT', true);
define('NO_DEBUG_DISPLAY', true);

require_once('/path/to/config.php');

$PAGE->set_context(context_system::instance());

$response = ['name' => 'guest'];
if (isloggedin()) {
    $response = ['name' => $USER->username, 'id' => $USER->id];
}

$result = json_encode($response);
if ($result === false) {
    print_error('generalexceptionmessage', 'core_error', '', json_last_error_msg());
}

header('Access-Control-Allow-Origin: *');

echo $OUTPUT->header();
echo $result;



 
Average of ratings: -
Picture of sara rabie
Re: ajax request not work in moodle
 
thank you for you reply but i use your code  but always display guest in two cases

the proplem i need from project in wordpress in the same server  to check if user login in moodle and return the name of user and i use this code

in wordpress in jquery
jQuery.post('/url/custom/ajax/check_login.php',  function(response) {
var res = jQuery.parseJSON(response);
if(res.name != 'guest'){
jQuery('#login_div').hide();
jQuery("#login_username").attr("href", "path./profile.php?id="+res.id)
jQuery('#login_username').text(res.name);
jQuery('#login_div_info').show();

}else{
jQuery('#login_div').show();
}

});
and this code work well on localhost but when upload it  on server it display cross origin block and fix it by header but it also not return any data about login user although he run files
 
Average of ratings: -
Picture of Darko Miletić
Re: ajax request not work in moodle
 
Average of ratings: -
Picture of sara rabie
Re: ajax request not work in moodle
 

i ty this but the moodle user data not retrive also but when i use old  version wordpress without new updates it work ajax  but my project  not work

 
Average of ratings: -