javascript - How to call ajax from anonymous host, header Access-Control-Allow-Origin: * not working

364

Hello i am trying to create a PHP page that can be called by ajax from anonymous host, I run my code XAMPP host and working nice, But when i try to run it fromhttp://podserver.info/ means I take a free hosting from there, it iswww.datatwo.podserver.info. I use the following PHP code

<?php 
    header('Access-Control-Allow-Origin: *');
    header('Access-Control-Allow-Methods: GET, POST, OPTIONS');
    header('Access-Control-Allow-Headers: Origin, Content-Type, Accept,Authorization, X-Request-With');
    header('Access-Control-Allow-Credentials: true');
    echo "hi";
?>

and JavaScript ajax code is

var xmlhttp;

    if (window.XMLHttpRequest) {
        // code for IE7+, Firefox, Chrome, Opera, Safari
        xmlhttp = new XMLHttpRequest();
    } else {
        // code for IE6, IE5
        xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
    }
    xmlhttp.onreadystatechange = function() {
        if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
            console.log(xmlhttp.responseText);
        }
    }
    xmlhttp.open("GET", "//www.datatwo.podserver.info/index.php", true);
    xmlhttp.send();

but it only work when I use XAMPP server and when I upload it to pod server it dos not work, what can I do now?

A error message from console XMLHttpRequest cannot load http://www.datatwo.podserver.info/index.php. No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http://stackoverflow.com' is therefore not allowed access.

491

Answer

Solution:

When you call ajax podserver don't give you main output from your PHP, server first convert your php to their format you can understand that using browserinspect element you noticed there some extra code added by podserver.

People are also looking for solutions to the problem: PHP with MS sql server

Source

Didn't find the answer?

Our community is visited by hundreds of web development professionals every day. Ask your question and get a quick answer for free.

Ask a Question

Write quick answer

Do you know the answer to this question? Write a quick response to it. With your help, we will make our community stronger.

Similar questions

Find the answer in similar questions on our website.