Ajax not working with this .htaccess - php

Im developing a CMS with PHP and MVC. I have the follow htaccess:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>
So i have a dispatcher to go to diferents views depends of the permalink on the url.
My problem is that im trying to use ajax for a Login and few things more and cant make it works.
Is there any expception in .htaccess to make this work or i only can put more exceptions on dispatcher to load the .php in ajax?
Anyone can help me?
Thanks in advance.
EDIT:
$.post
(
"../ajax/login.php",
{
u:Base64.encode(user),
p:Base64.encode(password),
},
function(data)
{
},
"json"
);
I tried with:
"../ajax/login.php"
PATH+VIEW+THEME+"/ajax/login.php"
My folder structure is:
view
themes
standar
ajax
login.php
js
ajaxInteractions.js
index.phtml

As stated in the comments, you need to check your path or just use your absolute path like this:
$.post
(
"/view/themes/standar/ajax/login.php",
{
u:Base64.encode(user),
p:Base64.encode(password),
},
function(data)
{
},
"json"
);
And one more thing; it is not always best practice to spread out the code like this ( it's good for PHP and plain JS calls ). For these types of calls, try to keep it more concentrated:
$.post( "/view/themes/standar/ajax/login.php", {
u:Base64.encode(user),
p:Base64.encode(password),
}, function(data) {
/** do your stuff **/
}, "json" );

Related

Laravel delete method not allowed

I am writing a laravel api and when I try to make delete and post requests I keep getting a method not allowed exception. Where should I be looking to diagnose this problem?
I have read though most of the other posts on this issue and here is what I have tried/looked at.
• Made sure the route syntax was correct
• Made sure it didn't conflict with another route
• Made sure I was using the correct route (ran php artisan route:list to
double check)
• Modified the .htaccess folder (maybe I did this incorrectly) to allow GET, POST, PUT, DELETE
Here is what the route looks like in api.php
Route::middleware('auth:api')->get('/user', function (Request $request) {
return $request->user();
});
Route::delete('delete/{id}', 'LoginController#delete');
Route::get('stuff', 'LoginController#index');
Route::get('stuff1/{Username}', 'LoginController#show');
here is the function in the controller
public function delete(Request $request, $id) {
$user = Login::find($id);
$user->delete();
return "204";
}
here is my .htaccess
<IfModule mod_rewrite.c>
<IfModule mod_negotiation.c>
Options -MultiViews
</IfModule>
RewriteEngine On
# Redirect Trailing Slashes If Not A Folder...
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} (.+)/$
RewriteRule ^ %1 [L,R=301]
# Handle Front Controller...
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ index.php [L]
# Handle Authorization Header
RewriteCond %{HTTP:Authorization} .
RewriteRule .* - [E=HTTP_AUTHORIZATION:%{HTTP:Authorization}]
<Limit GET POST PUT DELETE>
Allow from all
</Limit>
</IfModule>
I can get around this issue by changing Route::delete() to Route::get() and achieve the same functionality but this doesn't seem like standard practice.
You have to set ajax type POST but send a parameter named _method with value delete like this:
$.ajax({
type: "POST",
data:{
_method:"DELETE"
},
url: productRoute,
headers: { 'X-CSRF-TOKEN' : productToken }
});
Or because you use laravel html form helper so it generates _method hidden input automatically so you'd better send all your form inputs such as token and method like this:
function()
{
var formData=$('#yourForm').serialize();
$.ajax({
type: "POST",
url: productRoute,
data:formData
})
});

Rewrite URLs in RESTful API

I have to create an API in PHP, which I have never done before. I have a index.php in my root folder which contains a form. A button click activates an AJAX Request to read all entries and a form can be filled out to send data. I will also have DELETE and update.
ajaxcall.js
// GET to retrieve
var req;
req=new XMLHttpRequest();
req.open("GET", 'src/api/v1/posts',true);
req.send();
//post with ajax
$.ajax({
type:"POST",
url: "src/api/v1/posts",
data: test,
ContentType:"application/json",
success:function(){
alert('successfully posted');
},
error:function(){
alert('Could not be posted');
}
});
I have a folder src/api/v1 in there I want the endpoint file api.php which handles the different requests. I now need all the AJAX calls to be resend to api.php which sits inside that v1 folder. I created an .htaccess which is stored in also stored in src/api/v1. I added the following:
.htaccess
<IfModule mod_rewrite.c>
Options +FollowSymLinks
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule src/api/v1/(.*)$ src/api/v1/api.php?request=$1 [QSA,NC,L]
Unfortunately I get a 404 not found, it just does not seem to re-write the URL to the api.php so it is looking for /posts which does not exists. What am I doing wrong?
Actually you should store the .htaccess on the root directory, otherwise Apache will not know that there's an .htaccess under /src/api/v1, so apache will look for the directory src/api/v1/posts and as is it's not found a 404 error will be returned.
<IfModule mod_rewrite.c>
Options +FollowSymLinks
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule src/api/v1/(.*)$ /src/api/v1/api.php?request=$1 [QSA,NC,L]
</IfModule>

PHP routers, difference between "tutorial routers" and the big one from frameworks

it's my first post, so tell me if i've done something wrong.
i'm trying to understand logic of big routers on github and frameworks.
I understand to routers that works like this:
folder structure:
*controllers/Home.php
*index.php
index.php file, orientation code:
$uri = www.xyz.com/home/default/param;
# get parts from url
$parts = explode("/", $uri);
# call controller
$controller_class = "controller_namespace\\".$parts[0];
$controller = new $controller_class;
call_user_func_array(array($controller, $parts[1]), $parts[2]);
.htaccess file:
Options -MultiViews
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.+)$ index.php?url=$1 [QSA,L]
so on page www.xyz.com/home/default/param is called controller class home and func. default with params, in filestructure you are still on index.php file, only different controller classes is being called by url.
So, routers that i want understand works like:
class Home{
Router::get("/default", function(){
echo 'Hello';
});
Router::Dispatch();
}
From code that i found on github here etc. i think, the Router loads all the functions from controller and execute that one which match the url ...huh?
Q:
How i can execute the controller class above, when im still on index.php?

URL $_GET will not work

I have a problem I never had and I can't find the reason.
I moved my site to another host and now it doesnt "read" the $_GET variables.
I have this url: http://whatever.com/path?filtro=si&provincia=Santa+Fe&localidad=Rosario
And if I call this:
$localidad = $_GET['localidad'];
$provincia = $_GET['provincia'];
$filtro = $_GET['filtro'];
echo $localidad;
echo "hola";
echo $provincia;
echo $filtro;
Nothing prints except "hola", so there is no PHP error.
Here's my .htaccess:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>
Im working on a Wordpres site, perhaps it has something to do with Permalinks or something, Im really lost. Thank you very much, I appreciate your help.
EDIT
I renamed my .htacces so it wont read it and the page broke, so I went to permalink settings in wordpress and set them to
- Post Name http://luminias.com/index.php/example-page/
And now IT WORKS, but, now this is thw url:
http://whatever.com/index.php/path/?filtro=si&provincia=Santa+Fe&localidad=Rosario
And it prints all the $_GET, but I need that "/index.php/" gone..
Add add_rewrite_tag function in your function.php for all parameter :
function custom_rewrite_tag() {
add_rewrite_tag('%localidad%', '([^&]+)');
}
add_action('init', 'custom_rewrite_tag', 10, 0);
And you can call your parameter in template using
$wp_query->query_vars['localidad']
Here is the full documentation
Note that using $_GET on a rewritten URL will not work, even if the
rewrite includes the querystring variables. You must use $wp_query.

User Friendly Url Ajax post

Problem solved
I'm using friendly url and code which is described below. I want to Request php file with ajax post. Does anybody know how to do that?
For example: I have code below:
$(document).ready(function(){
$.ajax({
type: "POST",
url: "ajax/test.php",
data: "data=test",
success:function(data){
alert(data);
},
});
});
When program post data to test.php file then i want to also return data from this file.
Url: http://mysite.com/admin/admin.php?m=users&a=edit&id=12
User Friendly Url: http://mysite.com/admin/users/edit/12
Problem solved
I am confused what you are asking about. I will assume that you just want to return some data from test.php.
if so then assuming your $data variable holds all the data, you can do this
echo json_encode($data);
and that should alret you back the data as you have it in your code
<ifModule mod_rewrite.c>
RewriteEngine On
RewriteBase /directory_path/
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php/$1
</ifModule>
That's .htaccess file for user friendly url.
Thanks for help!

Categories