Comments (11)
Looks like it's working - I also updated the REAMDE 👍
from hit-counter.
If you look at the request being made to the server to get the count, is there a cookie being sent?
from hit-counter.
Hmmm, I don't know (I don't think so because I guess if it did the count would not increase? ^^)
How can I check this? The website is fully static and hosted on github pages
from hit-counter.
If you go to DevTools (in Chrome) and look at the request headers, you can see all the cookies sent:
from hit-counter.
Oh, haha I'm feeling so dumb to forgot we can see it there :D
Ok so, if I link an image, there's a cookie but in fact, when I'm just using an ajax request, there's none
from hit-counter.
Take a look at this SO question: Why is jquery's .ajax() method not sending my session cookie?
It is stated that:
AJAX calls only send Cookies if the url you're calling is on the same domain as your calling script.
I know this also happens in fetch, I have previously set credentials: 'same-origin'
to get around this.
If you try something like:
function updateHitCounterText(url, elementId) {
return $.ajax({
url: 'https://hitcounter.pythonanywhere.com/count',
data: { url: url },
xhrFields: {
withCredentials: true // <-- This guy should help
},
}).then(hitCount => {
replaceTextInElementById(elementId, hitCount);
});
};
does it work?
from hit-counter.
Encountering CORS origin conflicts, tried with fetch and credentials: 'include'
(since same-origin is deprecated) but having the same problem. Problem is that response header is containing Access-Control-Allow-Origin with wildcard *, which is not allowed when credentials are used ^^'
Also gave a try with ajax and dataType: jsonp
but returning error too since, obviously, returned data is not JSONp :)
Edit: this is the error message while using xhrField or credentials: 'include'
from hit-counter.
I found a workaround (kinda dirty, but working).
First, gratz for your very clear and straight forward doc about setting up project on python anywhere :)
Mounted my own setup so I'm not getting hitcounter busy especially with kinda useless requests.
Now, about the workaround, I make a request with image, which is hidden in DOM, then I make a request with nocount
to get the count and show it wherever I want while not increasing the counter :)
I guess I could make it cleaner by editing code and adding the wanted url as Access-Control-Allow-Origin, but since this is purely for personnal purpose and I'm lazy, I'll deal with it 😂
Here is how the final code is looking:
/**
* Creates a dynamic hidden hitcount image
* @param string hitCountDomain
* @param string url
*/
function createHitCounterImgElement(hitCountDomain, url) {
let imgElement = document.createElement('img');
imgElement.src = 'https://' + hitCountDomain + '.pythonanywhere.com/count/tag.svg?url=' + url;
imgElement.classList.add('hidden');
document.getElementById('body').appendChild(imgElement);
}
/**
* Replace target element inner html
* @param string elementId
* @param string text
*/
function replaceTextInElementById(elementId, text) {
document.getElementById(elementId).innerHTML = text;
}
/**
* Make ajax request and update hits counter
* @param string url
* @param string elementId
*/
function updateHitCounterText(url, elementId, count = false) {
const hitCountDomain = 'rdyx';
count && createHitCounterImgElement(hitCountDomain, url);
return $.ajax('https://' + hitCountDomain + '.pythonanywhere.com/nocount', { data: { url: url } }).then(
(hitCount) => {
replaceTextInElementById(elementId, hitCount);
}
);
}
Calling it with:
<script type="text/javascript">
postUrl = window.origin + '{{ site.baseurl }}/{{ post.url | remove_first: '/' }}';
updateHitCounterText(postUrl, '{{post.url}}-hits-count'); // Add true as last arg if you want to increase counter
</script>
from hit-counter.
I've been looking at this for a bit now and am a bit unsure on how to fix this.
In summary, we cannot set withCredentials: true
(credentials mode 'include') on a fetch/ajax call if Access-Control-Allow-Origin
or Access-Control-Allow-Headers
are set to *
. To allow any site to hit this website though, we need to set Access-Control-Allow-Origin: *
.
I do remember using flask-cors' supports_credentials=True
to do something similar recently - however, I imagine I set Access-Control-Allow-Origin
to something that was not *
which makes this useless.
from hit-counter.
Hehe yeah I get to the same statement, one workaround that may work (but I'm totally unsure if this is "clean" and I doubt it) would to get the origin from request (Host) and send it in the proper response header?
Else, I guess there is no real solution indeed, maybe the workaround I found earlier (using hidden image + nocount URL to get count and show it where I want) would be the best one. But doing so would double any counter request :)
from hit-counter.
Ah yes, the origin! Putting the origin in Access-Control-Allow-Origin
should work and defaulting it to '*'
if no origin is available. I'll look into this now, thank you.
from hit-counter.
Related Issues (20)
- Counter is broken HOT 3
- hit-counter gives timeout HOT 8
- Github shows You can't comment at this time.
- Your issue setting may be wrong. HOT 1
- Even the update of my comments is disabled (by settings?) :-(
- No 'Access-Control-Allow-Origin' header is present on the requested resource HOT 2
- The site is down HOT 5
- Feature request: allow to count files download HOT 3
- Hit Counter Badge isn't coming HOT 2
- Bug : Not Working (Doesn't Show up ) HOT 1
- Add a license HOT 1
- hit counter url is barely responsive HOT 2
- Deprecated Usage Examples HOT 1
- Is the same person viewing the repo again and again counted as another view? HOT 5
- How does counting for another site work? HOT 1
- Appears to be down HOT 1
- Feature request: unique visitor count HOT 2
- Hosted on #! HOT 1
- Is this still working now? HOT 1
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from hit-counter.