0

Im fairly new to post requests and ive been getting this error when i try to do a post. Ive looked for answers but everything is too complicated for me to understand what i need to change in my code for it too work.

var url = 'http://unturnedbox.clanservers.com/serverquery/serverQuery.php';
$(document).on('click','.test', function(getServerInfo)
{
   var getServerInfo =
   {
        "ip": "23.229.5.250",
        "port": "27021"
    }

    $.post(url, JSON.stringify(getServerInfo), function(response)
    {
        response.addHeader("Access-Control-Allow-Origin", "http://unturnedbox.clanservers.com");
        if (response.error)
        {

        }
        else
        {
            $('.test').html('<div>'+ response.result.length +'</div>');
        }
    });
});

Could someone please explain to me what im doing wrong. Thank you.

  • http://stackoverflow.com/questions/6114436/access-control-allow-origin-error-sending-a-jquery-post-to-google-apis – abigperson Dec 23 '16 at 00:35

1 Answers1

0

You cannot set the "Access-Control-Allow-Origin" on your javascript. That header is placed by the web server of the post call.

The server "http://unturnedbox.clanservers.com" have to set that header with the address/domain of your page, not you.

Example: if your website run at address http://www.myapp.com, the server have to reply with the header "Access-Control-Allow-Origin: http://www.myapp.com".

Kbyte
  • 138
  • 6
  • How would i do this? I own the unturnedbox.clanservers.com page – Aleksei ivanov Dec 23 '16 at 00:51
  • You can set the header in your webserver (examples http://enable-cors.org/server.html). You can also add the header in the response of http://unturnedbox.clanservers.com/serverquery/serverQuery.php page: http://enable-cors.org/server_php.html – Kbyte Dec 23 '16 at 00:58
  • Thank you, This worked. I added header('Access-Control-Allow-Origin: *'); – Aleksei ivanov Dec 23 '16 at 01:00