I am trying to get medicine name from TrueMD using api. I am using this from my localhost. For that reason web service security I also include 'Access-Control-Allow-Origin' as a null still same error of security. Is there somthing that I am missing?
$scope.medicines = [];
var search = "crocin";
var api_key = "API_KEY";
$http.get("http://www.truemd.in/api/medicine_suggestions?id=" + search + "?key=" + api_key)
.then(function(response){
response.header('Access-Control-Allow-Origin', "null");
$scope.medicines = response.data;
console.log($scope.medicine);
});
最佳答案
因为您的 GET 请求无效。当使用多个参数时,要使用的分隔符是“&”。
您的请求应该是: “ http://truemd.in/api/medicine_suggestions?id=crocin&key=XXXX ”
因此,您的代码应该如下所示
$http.get("http://www.truemd.in/api/medicine_suggestions?id=" + search + "&key=" + api_key)
.then(function(response){
...
}
PS:“Access-Control-Allow-Origin”是服务器端 header 。在请求中设置它不会执行任何操作。你可以摆脱它。
PS 2:您正在尝试更改服务器响应的 header 。这没有任何意义。我认为你误解了它的工作方式:(
关于javascript - 使用 Angular 获取数据 API 调用时出错,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36983609/