Jump to content

Recommended Posts

Posted

Привет Как написать запрос?

JS Code

$('#use').on('click', function() {
    console.log('on click #use');
    //    console.log('idres= ' + $(this).data('idres'));
    //    var id = $(this).data('idres');

    $.ajax({
        async: true,
        type: "POST",
        cache: false,
        data: {
            action: 'clickbtn',
            id: 100,
        },
        success: function(res) {
            console.log(res);
        }
    });
}); 

Code template 

?php 
                if ( $config->ajax ) {             
                  $action = $input->post->action;
                  $res = array();
                  if ($action == 'clickbtn') : // first ajax call uses POST method
                    $res = array ( 'id' => $page->id , 
                                   'title' => $page->title, 
                                   'action'=> $action);  
                    var_dump($res);
                    return json_encode ($res); 

                  else : // second ajax call uses GET method
                    $res['action'] = 'action';
                    $res['id'] = 'id';
                    return json_encode($res);
                  endif;
//                  return $this->halt();                
                } // не ajax, продолжаем обычный вывод страницы 
            ?>    

But it doesn't work, how to make it work?

Posted

Hello and welcome to the forum!

Please try to post in English if possible. Thank you.

Is your code inside a function? If not, you need to echo json_encode($res)

return json_encode ($res); // inside function
echo json_encode ($res); // not inside function

Do you see the output from var_dump() in the response in browser dev console?

  • Like 3

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...