Get Page URL after Ajax call has been made - ajax

I am using a ajax call to update page content and update the URL accordingly. I have share buttons on the page and when I want to share the whole page I only receive the previous loaded URL.
So as an example -
http://localhost/labs/category/best-of-the-best-campaign/
is my current loaded URL. When I do a Ajax call
http://localhost/labs/tag/ecommerce/?catid=2
This is the new URL. But when I share the page I still get the previous loaded URL. which is
http://localhost/labs/category/best-of-the-best-campaign
Could anyone point to me what might be going wrong?
<?php echo $url="http://$_SERVER[HTTP_HOST]$_SERVER[REQUEST_URI]";?>
This is what I'm using to get the current URL of the page. I have a doubt that it might be due to the server request but I am not entirely sure about it and how I might change it.
Any help will be greatly appreciated.
Thanks

I think you should reinitialize the $url variable after ajax call
You can write php code in your ajax sucess function like,
<script>
......// your code
success:function()
{
// your code
<?php
$url="http://localhost/labs/tag/ecommerce/?catid=2";
and use it.
?>
}
......// your code
</script>

Why don't you use location.href to get the url of the current page in JavaScript?

Related

Scala Play - calling controller method from view doesn't load a new page

I display information, gather some user's input, and call a controller method using ajax/java script call from the view. However, instead of displaying a new/different html page, the controller method returns back to the original html page that I called jscript from.
Is this because of ajax call? How do I make sure that controller method would display the html file it's supposed to, instead of staying on old html page?
It sounds to me like what you want to do is to open a different url than the one you are on. To do that you do not use ajax. Instead try using javascript to change window.location to tell the browser to go to the url (or 'call your controller'). It looks like this:
window.location.assign("/your/controller/url"); // or
window.location = "/your/controller/url";
There is more to read about it here: https://developer.mozilla.org/en-US/docs/Web/API/Window.location
You can think of ajax as a separate little browser that lets you call the web server but letting you handle what comes back with javascript rather than always showing it to the user. So if you want to do the call with ajax but get the response HTML into your page, you are responsible for taking that response and inserting it into the DOM.

load function after page switch on querymobile ajax

I have a jquerymobile ajax based web app. not using ajax is no option.
On one page a new script is inserted by <script url="../script.js"></script>
Now i would like to load that script, after the page is loaded by ajax. Unfortunately it does not happen. I tried for example:
$(document).live( 'pageinit',function(event)
CODE HERE
{});
But it does not work. As well as
$(document).bind('pageinit', function()
Any ideas how i can manage to load the function after the page is loaded by ajax??
Found the solution with help of "OMAR". I put the function inside the page div. But the link is not working, so i put the code itself into the top of the div. now it is working. not the best solution, but it is working.

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";
}

Upload local file contents without page refresh

I have a page in my web flow where I want to upload a file without refreshing the page. I tried using an Ajax call for that, but failed. I couldn't figure out how to send the data in the uploaded file to the server side/back end for further processing. I'm using the Spring MVC framework and I don't want to use PHP.
Can anyone suggest a solution or some sample code with which I can get my job done? I am very new to JavaScript.
One more thing is i have to get back to the same page after going to server side to process uploaded file and return to same page with a string from server side.all this happen without refreshing the current page
Any suggestion would be highly appreciated.
Assuming you've already gotten your form built and server-side controller set up to handle the upload, this little snippet should get you on your way to AJAX-y refresh-less file uploading glory!
//create a new FormData reference
//(note: you could use getElementById or querySelector)
var myForm = document.forms.myUploadForm;
var fd = new FormData(myForm);
//create and open an XHR
var xhr = new XMLHttpRequest();
xhr.open("POST","http://www.example.url/the/path/to/your/upload/controller");
//set up event listeners (optional)
xhr.onreadystatechange = monitorStatusFunction;
xhr.onprogress = updateProgressBarFunction;
//send the form (w/ no page refresh!)
xhr.send(fd);
I Struggled for lot many days and at last i figured out a solution which may not be the best solution but i am posting it here so that anyone like me can get help from this..
To meet all my requirements
I opened a jsp page as a popup from my jsp page and from that page i landed on my controller and done with my work on the server side and returned a string depending upon the content of the file uploaded to the same popup page and from the popup i transfered that string to my parent page.
Hence my work is done without refreshing the page...
Steps to do this
1.open a popup as follow
window.open('//url of jsp page to open','//somename','//properties of popup page');
2.land on your controller from the popup and return to the same jsp page with a string i want from the jsp page.
3.pass that string to the parent page as follow.
window.open.urparentjspfuntion(valuefromcontroller);
4.in parent page define a function to do what ever you want.....
I hope this solution may help so of those like me...

Rails 3 - automatic AJAX request

I am struggling with a problem about automatic AJAX load of page. I have normal static page, where is a link. After click on this link I will call an AJAX request for loading a data.
This works me. I try now to add next functionality - I would like after load a normal static page to load that AJAX data automatically - is possible to do somehow?
I tried it to do whole afternoon, but I still don't know, how to do it...
So, I will very grateful for every hints, how to do it... Thank you so much.
Make your Ajax call in your jQuery ready function, e.g.:
<script>
$(function() {
$.ajax(...);
});
</script>

Resources