SethGoodluck
SethGoodluck

Reputation: 390

Moving AJAX to server

Right now I have HTML blocks that involve a script to call data from another DB (outside of concrete5) and I've been using XHTTP Posts to retrieve the data.

The problem: The scripts currently exist on the client side (e.g. you can see it by inspecting element) and I think it is being called from the client's device. Once I close down access to that external DB, this won't work anymore. I need these requests to come from my website and not the client.

The need: I know there is a way to move these requests to within Concrete5, so that concrete5 is making the requests and not the client. I'm just not sure how to go about doing this. Any advice would be really appreciated.

Upvotes: 0

Views: 138

Answers (1)

Dominion79
Dominion79

Reputation: 77

You could do this by creating an end point.. which is an address on your website that returns data when called.

so for example calling

www.domain.com/api/getusers{id}

Could return a JSON object for users.

A quick example.

in your application/bootstrap/app.php file you can add.

Route::register(
  '/api/getusers/{id}',
  'application\Controller\endpoints::getUsers'
);

Then you add a controller in application/controllers/endpoints.php

<?php
namespace Application\Controller;

use Concrete\Core\Controller\Controller;
use \Concrete\Core\Http\Request;

class Endpoints extends Controller {

    /* Get all a Regions Expeditions */

    public function getUsers()
    {
      $id = Request::getInstance()->get('id');
      $userid = $th->sanitize($id);

            /* Do something with $id */

      echo json_encode($output);
    }
}
?>

More info can found here http://c5hub.com/learning/ajax-57-style/

Upvotes: 0

Related Questions