ajax calls - which path?

Post Reply
arfa
Master Bludit
Posts: 158
Joined: Tue Jul 04, 2017 4:31 am
Location: New Zealand
Has thanked: 13 times
Been thanked: 27 times

I am trying to make an ajax call inside a plugin but am not sure how to set the path.

Within <script> $(document).ready(function()
I am testing using...

Code: Select all

        $.ajax(
  'getData.php',
  {
      success: function(data) {
        alert('AJAX call was successful!');
        alert('Data from the server' + data);
      },
      error: function() {
        alert('There was some error performing the AJAX call!');
      }
   }
); // end ajax
I get the first alert but the data is the Bludit page sidebar with a 'page not found'

Does the call need to feed through plugin.php?
Or what am I missing? I am pretty sure it is path related but - perhaps not :)
Any thoughts would be appreciated.

Thanks - Kusalo
arfa
Master Bludit
Posts: 158
Joined: Tue Jul 04, 2017 4:31 am
Location: New Zealand
Has thanked: 13 times
Been thanked: 27 times

I really need some help here.
This is a Bludit specific issue and I really can't see a way forward.

assumptions:
1 - I want to get data added to the admin side of a plugin page. I am presuming to use ajax. Yes?
2 - There are security checks to be passed - in that I am not allowed to just make the ajax call and have the results appear (console, alert, add to #div).

2. seems to be the sticking point.
Scenario - jQuery notes the click and opens a hidden div - AND adds the ajax data to that div.
I tried so many path variations and a few of those give the console: "Not allowed to load local resource:"
Which has me thinking I need to set up something in plugin.php to placate admin.?
in adminView()?
if (isset($_GET['akAjax'])) { } // or some such????

It would be great to have some feedback here - even just to confirm or adjust my assumptions.
Feel free to treat me as a newbie and spell out the seriously obvious.
This reminds me of my earlier post trying to upload my first plugin on gitHub... the missing key? just add '/' after the 'new file' to create is a folder. Easy when you know that :)

I will add a few of my avenues of enquiry below. Perhaps not relevant/useful but I have been burnt on stackOverflow for not showing attempted approaches.
_________________
various paths with the token added

in: bl-kernel there is a folder 'ajax' - it seems to relate to...
bl-kernel/js/bludit-ajax.php
the first call there has: let url = HTML_PATH_ADMIN_ROOT+"ajax/save-as-draft"
this runs ajax/save-as-draft.php
Do I need to set something up for my ajax? I suspect not.

from a general online AI search I found:
url: "/bludit/admin/ajax/logo-upload", // Replace with the actual Bludit endpoint
I tried various path permutations

similarly from: https://git.leinelab.org/r31k/bludit/sr ... t-ajax.php
7 years ago..!
url: "<?php echo HTML_PATH_ADMIN_ROOT ?>ajax/save-as-draft"
I put a copy of my .php in the ajax folder and tried this. Nope.
I tried: bludit/admin/ajax/mySimpleTest.php and various other path alternatives
Nope.

searching the forum:
viewtopic.php?p=12050&hilit=ajax#p12050 - unresolved


I downloaded the Snicker plugin and tried to analyse the ajax calls - it is there but I couldn't tease out a simple line of action; my code knowledge limitations.

In docs.bludit.com I read through the API information and got:
fetch("https://www.example.com/api/pages/my-do ... 6f3eccc826"
the path???

Trying so many path variations...
quite a few of those give the console: "Not allowed to load local resource:"
Which has me thinking I need to set up something in plugin.php to placate admin.?
User avatar
novafacile
Master Bludit
Posts: 130
Joined: Sat Oct 06, 2018 4:47 pm
Has thanked: 63 times
Been thanked: 17 times
Contact:

You can check how I did it in the ImageGallery. The image uploads and some other settings are working with Ajax requests.

I did write an own Ajax handler for this plugin, but you can use it as starting point for your own needs.
arfa
Master Bludit
Posts: 158
Joined: Tue Jul 04, 2017 4:31 am
Location: New Zealand
Has thanked: 13 times
Been thanked: 27 times

@novafacile
I really appreciate the response and your suggestion. I have the pro version and enjoyed digging into your ajax offering - but... it is a bit outside my pay grade :) {read: complicated} - I am also surprised that there is not a simple, in-house, way of dealing with this. An ajax call is a pretty common, standard thing. Sure admin needs to be involved but... ???
So, I have a working setup - that I was using as an interim but, in light of the complexities you introduce, may prove to be my on-going. It was a bit of a sweat getting to this point and I would be really interested in feedback on what I think of more as a hack than a solution. Do allow that I have stripped things down here somewhat but hope that the logic is clear.

I set up my divs -
1 = #clickMe
2 = #stickItHere

I click...

Code: Select all

<script>
$('body').on('click', '#clickMe', function() {
    $.get("<?php echo DOMAIN_ADMIN; ?>
            plugin/myLatestThing?&getTagAjax&folder=
            "+folder, function(folderData) {
    match = folderData.match(/<!--start-->([\s\S]*?)<!--end-->/);
$('#stickItHere').html(match[1].trim());
            });  // end get
}
</script>
in plugin.php - public function adminView()

Code: Select all

<?php 
if (isset($_GET['getTagAjax'])) {
    include($this->phpPath() . "php/ajaxTags.php");
}
?>
the problem with "include($this->phpPath()" is it returns a whole page - ajaxTags.php data + the sidebar. The sidebar code is a pretty light load so, no big deal. Carry it.

in ajaxTags.php - NB. note the start end comments! pivotal

Code: Select all

<!--start-->
<?php 
if (isset($_GET['folder'])) { $folder = $_GET['folder']; }
else { $folder ='$_GET not defined'; }
// do a bunch of stuff
echo $stuff;
// or this can be several blocks to test various calls...
if (isset($_GET['oneBlock'])) { echo 'oneBlock'; }
if (isset($_GET['twoBlock'])) { echo 'twoBlock'; }
?>
<!--end-->
so, the $_GET is recognised by admin and feeds to ajaxTags.php just fine.
the script gets the return data = folderData
it strips out the data between the comments ... just what I want.
sticks it in #stickItHere - Done.

With this set up each ajax call just requires its own feed in plugin.php and it can use the same file to handle the $_GET info. - with multiple $_GET tests.

Bottom line... it works. It is relatively clean and simple. As I said, I would really appreciate any comments on this. Limitations? Security issues? What come to your mind?

lets boogie - Kusalo
Post Reply