http总是调用错误函数

时间:2014-09-15 18:47:24

标签: angularjs

这是我的代码片段。似乎一切正常,但我不知道为什么,我总是得到错误2警报。 URL是100%工作,它返回unicode

中的字符串数组
App.service('categoriesService', function($http) {
    this.getData = function(callbackFunc) {
       return $http.get('http://yuppi.com.ua/rest/categories.php?key=453sdfg3t').error(function(data) { alert("error2");});
      }
});

[{"id":3,"name":"\u042d\u043b\u0435\u043a\u0442\u0440\u043e\u043d\u0438\u043a\u0430","count":"4265"},{"id":6,"name":"\u041a\u043e\u043c\u043f\u044c\u044e\u0442\u0435\u0440\u044b \u0438 \u041d\u043e\u0443\u0442\u0431\u0443\u043a\u0438","count":"6424"},{"id":11,"name":"\u0422\u0435\u043b\u0435\u0444\u043e\u043d\u044b \u0438 \u041f\u043b\u0430\u043d\u0448\u0435\u0442\u044b","count":"4882"},{"id":5,"name":"\u0414\u043e\u043c \u0438 \u0441\u0430\u0434","count":"8008"},{"id":4,"name":"\u0424\u043e\u0442\u043e \u0438 \u0412\u0438\u0434\u0435\u043e","count":"7434"},{"id":2,"name":"\u0422\u0440\u0430\u043d\u0441\u043f\u043e\u0440\u0442","count":"7440"},{"id":10,"name":"\u041d\u0435\u0434\u0432\u0438\u0436\u0438\u043c\u043e\u0441\u0442\u044c","count":"1891"},{"id":7,"name":"\u041c\u0443\u0437\u044b\u043a\u0430\u043b\u044c\u043d\u044b\u0435 \u0438\u043d\u0441\u0442\u0440\u0443\u043c\u0435\u043d\u0442\u044b","count":"6653"},{"id":8,"name":"\u0425\u043e\u0431\u0438, \u043e\u0442\u0434\u044b\u0445 \u0438 \u0441\u043f\u043e\u0440\u0442","count":"3597"},{"id":9,"name":"\u0422\u043e\u0432\u0430\u0440\u044b \u0434\u043b\u044f \u0434\u0435\u0442\u0435\u0439","count":"7417"},{"id":12,"name":"\u0416\u0438\u0432\u043e\u0442\u043d\u044b\u0435","count":"3775"},{"id":13,"name":"\u041c\u043e\u0434\u0430 \u0438 \u0441\u0442\u0438\u043b\u044c","count":"5816"},{"id":14,"name":"\u0420\u0435\u043c\u043e\u043d\u0442 \u0438 \u0441\u0442\u0440\u043e\u0438\u0442\u0435\u043b\u044c\u0441\u0442\u0432\u043e","count":"4516"}]

2 个答案:

答案 0 :(得分:1)

很明显,您正在点击错误功能,因为来自该网址的响应并不包含CORS标头,以使您的浏览器能够将请求限定为允许

你可以在这里看到小提琴:http://jsfiddle.net/frbrn32d/

您可以阅读跨域请求,以更好地了解正在发生的事情。此外,要解决这些问题,最好对API进行更改以发送相应的标头。可以暂时禁用特定于浏览器的CORS安全检查,但这不是解决方案。

这是测试代码;

var app = angular.module("TestApp", []);

app.controller("TestCtrl", ["$http", "$scope", function($http, $scope) {
    $scope.msg = "Hello, world!";
    $http.get("http://yuppi.com.ua/rest/categories.php?key=453sdfg3t")
    .success(function (response) {
        console.log("RESPONSE", response);
    })
    .error(function (err, status, headers) {
        console.error("ERR", err, status);
    });
}]);

当您启动javascript控制台时,您将看到错误消息。

答案 1 :(得分:0)

如果您有CORS问题,请将此标头添加到php文件header("access-control-allow-origin: *");