Posts: 47
Threads: 7
Joined: Dec 2016
Hi, I have a problem with AJAX that occurs only in GetSimple.
I have a page the URL of which in browser's address bar is (schematically): http://localhost/installation-folder/parent/page1/ and I want to insert a content in it (after click event) from the file the location of which is (schematically) installation-folder/data/pages/page2.xml. So, I address the source file with ".../.../data/pages/page2.xml" in my script. But if the click event occurs, nothing happens, only an error appears in the browser's console which reads: POST XHR http://localhost/nts/data/pages/f55.xml [HTTP/1.1 403 Forbidden 0ms]. If I tested the script with the same files on localhost ouside GetSimple or in a server in internet (also outside GetSimple) it worked fine. All files in the data/pages folder have permissions 755. I have no idea what can be the cause of this malfunction.
Posts: 6,266
Threads: 181
Joined: Sep 2011
There is no such thing as localhost, use IP
Posts: 538
Threads: 12
Joined: May 2013
Why would you need access to an xml file within javascript, it's a mystery to me, what this is good for? Or did I understand that incorrectly? You cannot access xml file using its url, you do not have access to the xml files, restricted with .htaccess, which is a good thing.
Posts: 6,266
Threads: 181
Joined: Sep 2011
ohh xml file, all xml files are blocked via the htaccess rule, you have to add an exception, also data files are protected you should serve your data up through json via a processor, public access to data files is not a good idea.
Posts: 47
Threads: 7
Joined: Dec 2016
(2017-08-13, 00:59:37)Bigin Wrote: Why would you need access to an xml file within javascript, it's a mystery to me, what this is good for? Or did I understand that incorrectly? You cannot access xml file using its url, you do not have access to the xml files, restricted with .htaccess, which is a good thing.
I want display the content of another page (without its header, sidebar, footer) in a page when some javascript event occurs. In what different way can I achieve that than read the content of the respective page's xml file with AJAX?
Posts: 538
Threads: 12
Joined: May 2013
2017-08-14, 19:20:12
(This post was last modified: 2017-08-14, 19:20:36 by Bigin.)
There are many ways to achieve that. The one option would be to call a processor function via Ajax. This function returns you all desired details of your page. The content can be sent and received in the json format, for instance.
First off, the ajax call in your template file:
Code: var slug = 'your_slug';
$.ajax({
url: 'http://your-domain.com',
type: 'GET',
data: {
'getPageContent': slug
},
dataType: 'json',
success: function (data) {
if(data) {
console.log(data.content);
}
}
});
Your controller, a few lines in the functions.php:
PHP Code: if(!empty($_GET['getPageContent'])) { echo json_encode(your_function($_GET['getPageContent'])); exit(); }
Your processor (could be a function in your functions.php file):
PHP Code: function your_function($slug) { $data = null; $id = null; $id = lowercase(str_replace('/', '', str_replace('..', '', $slug))); if($id) { $data = getXml(GSDATAPAGESPATH.$id.'.xml'); } return isset($data->content) ? array('content' => (string)$data->content) : null; }
Posts: 35
Threads: 7
Joined: Oct 2011
2017-08-19, 07:19:23
(This post was last modified: 2017-08-19, 07:20:36 by roy_b8te.)
(2017-08-14, 19:20:12)Bigin Wrote: Your controller, a few lines in the functions.php:
PHP Code: if(!empty($_GET['getPageContent'])) { echo json_encode(your_function($_GET['getPageContent'])); exit(); }
Your processor (could be a function in your functions.php file):
PHP Code: function your_function($slug) { $data = null; $id = null; $id = lowercase(str_replace('/', '', str_replace('..', '', $slug))); if($id) { $data = getXml(GSDATAPAGESPATH.$id.'.xml'); } return isset($data->content) ? array('content' => (string)$data->content) : null; }
I'm trying to tackle something similar to this. I'm wondering if I can pass the slug w/o the controller?
Thanks for any info.
Posts: 538
Threads: 12
Joined: May 2013
Normally, no physical controller is necessary here, it's just a simple if construct - I call it controller - it allows for conditional execution of your_function(), in this example
Posts: 35
Threads: 7
Joined: Oct 2011
Thanks, that confirms my tests without the construct.
In the bigger picture, I'm not able to return data from the AJAX request as it's calling a .php script with the defined('IN_GS') directive in the template folder.
Passing a parameter to a function within the template function.php script does seem to work,
but it's a pretty ugly client side hack.
Code: // simple client example
submitrequest = function()
{
var val = "some front end value";
<?php $phpval = '" + val + "'; ?>
console.log( "<?php returnvalue( $phpval ); ?>" );
}
I'm also not looking forward to other post processing that would be handled with a simple AJAX call.
I'm considering trying to place the AJAX script in a sub folder of template and adding an .htaccess with exceptions to allow it to be called unless there's something I'm missing here...
As of now getting this to work in a straight forward way has me kinda of stumped.
Still open to any other possible approach or idea..
Posts: 538
Threads: 12
Joined: May 2013
The file is called "functions.php" not "function.php" and it automatically included by GS every time a page is loaded on your site.
Code: <?php $phpval = '" + val + "'; ?>
It may be that you are trying to mix PHP and Javascript here, it will not work...
Posts: 6,266
Threads: 181
Joined: Sep 2011
Slug is usually global $id although I think there is wrapper function for that
Posts: 35
Threads: 7
Joined: Oct 2011
2017-08-20, 20:49:09
(This post was last modified: 2017-08-20, 21:15:01 by roy_b8te.)
(2017-08-19, 18:25:32)Bigin Wrote: The file is called "functions.php" not "function.php" and it automatically included by GS every time a page is loaded on your site.
It may be that you are trying to mix PHP and Javascript here, it will not work...
Sorry function.php was a typo.
I am able to pass a value using the convoluted example I posted,
I'm just trying to figure out how to submit a normal AJAX request.
|