What I'd like to do is have a button that you can click to refresh the comments section without reloading the page.
This seems like it should be simple to do, but I just can't get my head around Ajax.
(no button just yet, just an interval)
setInterval(function(){
$(".commentswrapper").load("html");
}, 10000);
Failed to load resource: the server responded with a status of 404 (Not Found)
localhost:8888/post-title/html
(http:// omitted from link because SO won't let me wrap it in code tags or post it otherwise)
Shouldn't that just refresh the html of that div?
The jquery load method requests a page of HTML and puts it in your div. the parameter you give is a URL from your server. In your example, it is requesting a URL of "HTML", which doesn't exist in Wordpress.
To get this working, you would have to write a new server script, e.g. Getcomments.php which returns the comments for the current page.
This isn't trivial unless you've done some server side programming in php and are familiar with the Wordpress codex,
Related
The flow looks like that:
user is filling the form
form is passed to the server by ajax
form is saved to db, then the pdf with the form data is created and saved in the app folder (probably a bad move here...)
ajax success causes the page to append a button 'Download' with value equal to current pdf's name so button 'Download' appears to the user
If user presses the button the very pdf that was just saved is gonna download.
Refreshing the page makes the button disappear.
I've got stuck on point 5. I have created another ajax (to avoid reloading the page) bound to the Download button. It correctly asks the server to look for the file, creates a django File object: pdf_file = File(open(file_path, 'rb'))
and creates a HttpResponse with file, and content_type='application/pdf' or 'application/download'.
response['Content-Disposition'] is attachment.
Then the ajax returns response - only it does not. Server raises no error but ajax error function is called.
I've read that downloading with ajax is not possible. Could you help me a bit to get it straight? If above snippets are not clear, I shall provide more code.
Python 3.5, Django 1.10
Do like
window.location.href = "/url/to/downloadfile/"
in javascript after success of posting form. OR
#html
<button onclick="myfiledownload()">Download</download>
#javascript
function myfiledownload(){
window.location.href = "/url/to/downloadfile/"
}
Instead of using ajax to download the file bind the button to a download link where the file may be hosted
https://www.mywebsite/download/?fileid=3247023
You should at least seperate your file in a media root
Note: in production you will have to use a cdn to host your static files
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";
}
My web app runs under Tomcat, it uses AJAX requests very intensively, and during the development process I have to redeploy the web app intensively too. After the redeployment I usually simply refresh the page knowing that the user session is dropped, but I always get to the scenario described below:
Go to some page, a really big page with many JS-scripts included, that actually makes those intensive AJAX requests.
Stop Tomcat or redeploy the web app.
Refresh the page.
Enter the credentials in the sign-in form to authenticate.
Suddenly get the last AJAX request response in the browser window and the AJAX request URL in the URL bar.
Wow... It looks strange for me that AJAX request URLs appear in the URL bar along with their response in the web page display area. This actually happens to Firefox and Chrome (haven't tested it in other browsers). Unfortunately, I cannot reproduce the same behavior in a simple page written from scratch. Frankly speaking, I don't really understand what happens to such requests and why do browsers "think" if AJAX requests/responses are entered in URL bar by user...
Your possible explanations or hints for such a strange behavior are very and very appreciated. Thanks!
(Perhaps it can help: All AJAX requests are performed with jQuery 1.4.2)
I'm very sorry, as I understood later, the question is not complete, because there was another pitfall I didn't mention, so no one couldn't answer the question in principle. The reason of the bug was hiding in the... <body onunload="..."> tag definition. That was quite unexpected, but that code contained some AJAX request that must be invoked when a user leaves the page. I only realized that the authenicating module (FORM, j_security_check) returned a response (HTTP 302) containing the Location header - so that was the reason why browsers did redirects.
The exact scenario was like that:
Open the page containing <body onunload="some_ajax_here">.
Log out the app using another tab so you could stay at the same page.
Refresh the page so some ajax could be invoked - this AJAX request is not now allowed because it's a secured part of the application (you get the forwarded content of the login page).
Enter the credentials and now oops you get to the result of some ajax directly in the browser window.
As the quick fix, and I hope the final one, I added another request following that AJAX request:
$.ajax({
async: true,
method: "GET",
url: document.location.pathname + document.location.search
});
So the HTML page script simply makes self-page request the last one - j_security_check returns the Location referring the last used HTML page, and the redirect works fine. Perhaps my explanation is not clear and may be not complete or even full of mistakes, but it looks like that in general. I'm very sorry once again, and thank you #ChristopherSchultz!
I'm having an issue with Buddypress ajax calls. The calls themselves are being made, however it's on the callback i'm having problems. I'm using a custom theme, with some custom pages for buddy press. All the same javascript files are loaded and the ajaxurl is defined as normal.
When i interact with anything in Buddpress (favourite, post message, comment etc) I get a 404 on wp-load.php in console. The content is there. The ajax function works itself as if you refresh the page the content is updated.
Tried a few things to fix up but getting nowhere.
After agonising on this for a while, there was a simple fix which i placed in the Ajax.php file.
status_header( 200 );
Is it possible to render another page instead of one currently being processed?
For instance, my plugin detects 404 error, sets corresponding HTTP headers, and then outputs a normal page from website - but under those HTTP 404 headers, so the browser and search engines will receive custom, good-looking 404 page.
Redirecting by
<? header("Location: ...'); ?>
is not an option. I need stop rendering current page and render another, but display it under this, non-existing url.
How this can be done?
Sounds like you need a system event plugin, please see:
http://docs.joomla.org/Plugin/Events/System
in particular, the onAfterDispatch method:
http://docs.joomla.org/Plugin/Events/System#onAfterDispatch
The onAfterDispatch method is called after routing, and after Joomla has determined whether the url is valid, so it should be the time to get involved and redirect to another page.