Question

Haven't had much luck with my keywords in Google or here so I'm asking this even though I am sure its trivial...

I want to use a JQuery $.POST method to ask for information from a controller that is located above the server root... I have found answers in .ASP and J2EE but not simple PHP/HTML/etc...

This controller does not need to be instantiated so the simple URL should be enough to send it info and ask for a response. I have used JQuery a long time so I don't need answers that ask if I am using .POST correctly as I have used this AJAX method for over two years.... Just not in MVC architecture.

I have a PHP MVC built site that I am running and I basically just want to get some info from my controller so I am trying this $.post("../../../controller/controllerScript.php", {...}, function() {...}) ... I also have tried removing each of the ../ individually and tested each. No luck... So far I am getting consistent error 404 messages that it can't find the file. This makes sense to me because this is above the server root and shouldn't be publicly accessible.

I am aware that this is because I am looking for a file above the server root but I thought that was supposed to be the magic of MVC that I can set the root to the public folder and have a lot more security and clear programming. I know the problem probably lies in needing an .HTACCESS file which I suck at doing, or some other URL redirection method but I just don't know which.

My server folder architecture

-controller
    -fileINeed.php
-model
-public
    -js
        -myPOSTfunction
-view

I thought a simple action="../../../controller/fileINeed.php" would at least let me submit the form to an empty page so I could debug it but even this cant be found. It changes the URL to //BASE/controller/fileINeed.php which I am sure is part of the server security to files above the root.

Anyone able to quickly give me a MVC-101 schooling and show me how dumb I am? lol

Was it helpful?

Solution

The issue is JQuery is a client side language, thus is restricted to the document root and cannot traverse above, this is a security feature because if a JQuery could traverse above root so could your visitors and this would cause major security issues. Depending on how your server is set up you can use the JQuery to call a php or any other server side application that resides inside the root then inside that script make a call or reference a app located above it.

Create a caller php inside public\ then include it from there

include('/home/controller/fileINeed.php');

Keeping in mind you need to figure out the directory structure between your server root and the public folder and replace /home with it. some use /www.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top