Joomla redirect after ajax request fails - ajax

An ajax request calls a controller function within my MVC Component.
And in this function there is a Joomla redirect at the end with
JFactory::getApplication()->redirect(JRoute::_($redirect_url, false));
The problem is, that the redirected page won't open! While debugging a session, I can see that the controller will initiate the redirect and the proper details will be sent to view.html.php. But the page, in this case it is a different view in tmpl, will not be open!
Is this because of the ajax call? Do I have to do something in the ajax success part?

Yes, now I do the url preparation in the controller and send back the url to ajax.
$redirect_url = "index.php?option=com_mycomponent&view=my_view&layout=different_layout"
Ajax will do the redirect with the given url.
window.location = result.data;
From there iam struggeling with the SEF part of Joomla to get a SEF friendly URL.

Related

Laravel route after post in same page ""

We are using Laravel, we have a form post on the "/",
$router->get('/', 'AppController#show');
The form is a script that appear through a modal window, once we post the form it uses route:
$router->post('/', 'AppController#store');
and function
public function store()
{
return view('app');
}
How we can stay on page without any return action after the post ?
With the current method it reloads the "/" page,
Are there a way to keep the "/" page as it is without reloading after the submit button?
Apologise for my english, i hope you understand our issue.
Thanks in advance,
You will need to make an ajax call to that resource. You can do with with plain javascript or you can use jQuery for this. $.post('/').
Dependant on your version, version 5.4 has a covenant axios.post method that you can use with Vue to send your ajax requests.
For a simple app jQuery may be the best simple option for you.

Handling redirects with Codeigniter and jQuery mobile?

I'm building a mobile web app with jQuery mobile and Codeigniter & TankAuth. When a user logs in or out, they are redirected from the TankAuth login/logout controller to a page of my choice. The problem is that while the ajax navigation loads the page in question, the URL to the TankAuth controller stays the same.
There is quite a lot of code involved, so I will describe it the best I can:
Consider the variable $this->session->flashdata('prev_page') is page1.
The TankAuth login controller is called first time via auth/login, loads login form view.
The form is submitted to the same controller which then processes it and redirects to necessary page:
if ($this->tank_auth->login(
$this->form_validation->set_value('login'),
$this->form_validation->set_value('password'),
$this->form_validation->set_value('remember'),
$data['login_by_username'],
$data['login_by_email'])) {// success
redirect($this->session->flashdata('prev_page'));
}
jQuery mobile loads page1 via AJAX but the URL remains as auth/login.
How do I fix this? Please let me know if you need any more info.
If my understanding about your problem is correct, you are using ajax for authenticating mobile users and normal redirect method for big screen users.
If that's the situation, then this will do the magic:
if ($this->tank_auth->login(
$this->form_validation->set_value('login'),
$this->form_validation->set_value('password'),
$this->form_validation->set_value('remember'),
$data['login_by_username'],
$data['login_by_email']))
{// success
$this->load->library('user_agent');
if($this->agent->is_mobile()) {
echo $this->session->flashdata('prev_page');
//Now this value 'll reach ajax when authentication is success.
//Inside Ajax:
//success: function(data) {
// validate data for checking if user is logged in or not.
// if(login_is_success)
// window.location.href = data;
//}
}
else
redirect($this->session->flashdata('prev_page'));
}
I added data-url="true" to the the form in opening <form> tag in question, everything worked fine from there.

CakePHP redirect entire page instead of div

When I'm logging in with my CakePHP login function in the Users controller using ajax. The errors will be displayed in an Div above the login form. But when I have a successful login, I want to redirect the page to the homepage. When I'm using $this->redirect('home'); in the Users controller, the Div will only be updated.
How can I make sure that the entire page will be reloaded? I rather fix this problem with PHP than Javascript 'cause I'm using the Writebuffer method with the JSHelper.
Thanks!
This should be solved in client side. Because client browser gets the AJAX response and puts the output inside the DIV.
Although you can embed javascript code inside the AJAX response. For example, if login is incorrect, then put a window.location javascript code inside the output. But this is not a good solution.
Return an HTTP status code within AJAX response. If for example code is 401, then redirect.
if (data == '401') {
document.location.href="http://example.com";
}

Ajax load a page like an include

I have a problem: I need to load a set of includes with ajax, but when I make the ajax
and request the current URL, I get the page url.
I have a problem when I call a page via ajax; the problem is when I try to get the request
URL in the page which is called with ajax, because it doesn't work like an include.
For example: I have my page "prueba.adp" and "prueba-ajax.adp"
The page prueba.adp calls via ajax to prueba-ajax.adp.
So when I try to get the request URL in prueba-ajax.adp I get prueba-ajax.adp, but
if I set an include I get prueba.adp.
Do you know any way to execute the ajax page and get the request url of the page which made the ajax call?

ASP.NET MVC3 - Action without reload site

I have MVC3 app with form for edit data. This for include only dropdownlist.
It is any posibility to execute Controller method (HttpPost) without reload?
If not - how Can I return current site (because I have the same form in different sites).
Yes, try XmlHttpRequest better known as AJAX to execute controllers. It's like sending a request from the browser to the server on a background thread which won't cause a page reload.
have a read of these blogs:
http://www.nikhilk.net/Ajax-MVC.aspx
http://dotnetslackers.com/articles/aspnet/ASP-NET-MVC-2-0-and-AJAX-Part-1.aspx
If you can't use ajax, to get back to the same page you can redirect the user back to the referrer url:
public ActionResult Submit()
{
// do something
return Redirect(Request.UrlReferrer.ToString());
}

Resources