1

I am trying to get data from a php script to my angular app. I am using this code to get the data. However it fails and then gives me no information why.

var req = {
        method: 'GET',
        url: 'http://www.hott-source.com/hangman/getMemory.php',
        data: { }
    }

    $http(req).then(function(response){
        alert("WINNER");
        $scope.knownWords = response.data;
    }, function(response){
        alert(response.data);
    });

The PHP script returns

[
    ["A","I"],
    ["BE","BY","DO","GO","IN","ME","MY"],
    ["CAT","COW","DOG","GOD","HIT","MAT","PAT","PIG","RUN","YOU"],
    ["BAND","BIRD","CART","CHIP","DOVE","JUMP","LOVE","READ"],
    ["CHIRP","HAPPY","HORSE"],
    ["CHURCH","HANSON","HITTER","PEOPLE","PRIEST","STRIKE","THOMAS"],
    ["BELINDA","BUILDER","SHOOTER"],
    ["CLAPPING","ELEPHANT"],
    ["COLLECTION"]
] 

which I have verified as json. In the php file I set the header Content-Type to json just before I send it

header('Content-Type: application/json');
echo json_encode($newArray);

response.data alerts as null

How do I get the data from the php script into my $scope.knownWords

6
  • 1
    Did you make request from http://www.hott-source.com/ or your js located on another domain? Commented Mar 1, 2016 at 9:10
  • Try with console.log instead of alert and check the logs in the developer tools. Commented Mar 1, 2016 at 9:11
  • @BobSponge The app is running on my computer. The server is a server I rent elsewhere. Commented Mar 1, 2016 at 9:16
  • @cezar what would I put into the console.log? nothing has any data in it. Commented Mar 1, 2016 at 9:17
  • 1
    So, getMemory.php must return access-control-allow-origin: * http header. More info here: stackoverflow.com/questions/10636611/… Commented Mar 1, 2016 at 9:18

1 Answer 1

1

Script getMemory.php must return access-control-allow-origin http header:

header('access-control-allow-origin: *')

More info about CORS here: How does Access-Control-Allow-Origin header work?

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.