gpt4 book ai didi

javascript - AngularJS http php 回显

转载 作者:行者123 更新时间:2023-11-27 23:17:18 26 4
gpt4 key购买 nike

我正在尝试将数据从 php 脚本获取到我的 Angular 应用程序。我正在使用此代码来获取数据。然而它失败了,然后没有给我任何信息。

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);
});

PHP 脚本返回

[
["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"]
]

我已经验证它是 json。在 php 文件中,我在发送之前将 header Content-Type 设置为 json

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

response.data 警报为空

如何将 php 脚本中的数据获取到我的 $scope.knownWords

最佳答案

脚本getMemory.php必须返回access-control-allow-origin http header :

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

有关 CORS 的更多信息:How does Access-Control-Allow-Origin header work?

关于javascript - AngularJS http php 回显,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35718603/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com