Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7651457
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T11:30:43+00:00 2026-05-31T11:30:43+00:00

I made a ‘voting’ API for a topsite for the sites registered to use.

  • 0

I made a ‘voting’ API for a topsite for the sites registered to use.

<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js"></script>

    <script type="text/javascript">

        var id = 1;

        $(document).ready(function(){
            $.getJSON("http://mysite.com/index.php?page=vote", { id: id, hasVoted: 'unknown' }, function(data) {
                if(data == 2) {
                    window.location.replace("http://mysite.com/index.php?page=vote&id=" + id);
                }
            });
        });

    </script>

Basically, I give the user’s their ID and then they put that code in their files.
The site returns a number and upon that, it redirects the user or not to voting.

So, testing on localhost that code throws this error:

 XMLHttpRequest cannot load http://mysite.com/index.php?page=vote&id=1&hasVoted=unknown. Origin http://localhost is not allowed by Access-Control-Allow-Origin.

If I use this code:

<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js"></script>

    <script type="text/javascript">

        var id = 1;

        $(document).ready(function(){
            $.getJSON("http://mysite.com/index.php?page=vote&callback=?", { id: id, hasVoted: 'unknown' }, function(data) {
                if(data == 2) {
                    window.location.replace("http://mysite.com/index.php?page=vote&id=" + id);
                }
            });
        });

    </script>

Then it does nothing. It throws no error. ( While data is supposed to be equal to 2 ( When checking that URL directly, it returns 2 ) ).

And, if I try to do a little test, and do alert(data); It throws nothing still.

I’m completely clueless about this. Anything will help.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-31T11:30:45+00:00Added an answer on May 31, 2026 at 11:30 am

    Well known problem, the origin control of the browser:

    A browser doesn’t allow you to make ajax requests to ‘cross-origin’ domains by default. Cross origin means, that either the port or host is different. Therefore you can’t send requests to site http://siteB/ while the script to make the requests is hosted on site A.

    Simple solution would be to host everything on one domain and make the url relative. If that’s not possible, you have to find another way no make cross-origin calls.
    Some examples:

    1. JSONP (only GET)
    2. CORS (work’s very good with modern browser)
    3. include an ifram + use PostMessage between the windows
    4. proxy script

    EDIT:
    What I would do, is first allow your API-SERVER some stuff, by setting these headers:

    header("Access-Control-Allow-Origin: *");
    header("Access-Control-Allow-Methods: POST, GET, OPTIONS");
    header("Access-Control-Allow-Headers: Authorization");
    

    Be aware, that this isn’t working properly with the Internet Explorer, so you have to use: XDomainRequest. But this doesn’t allow you sending headers. Please see my question a couple days ago, if you have to send headers cross-origin

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I made an html file called test.html then I navigated to it as http://site.com/test.html?test1=a
I made a script which iterates through a couple pages of a third party
Made this custom alert box: <script type="text/javascript"> $(function () { var $alert = $('#alert');
Made this script to crawl certain links on a forum and extract the username,
Made a simple script that hide/show a hidden div. The page automatically adjusts its
Edit: Made an official bug report at developers.Facebook.com GETing either of the following: graph.facebook.com/me/photos
I made a website that depends on PHP, AJAX and Javascript. The problem here
Made solution change: I am trying to display a html table of data.. In
Made this nice little loop for hiding and showing div's, works as a charm
I made a view to abstract columns of different tables and pre-filter and pre-sort

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.