当我用angular和restangular执行getList时出错了

时间:2013-10-14 16:07:50

标签: angularjs restangular

大家好我正在开发一个应用程序,后端用Wordpress制作并带有休息api。

但我无法解析响应,我不知道为什么,首先我认为这是json响应,这就是为什么我实现了setResponseInterceptor方法。

这里的问题是我不确定是否setResponseInterceptor,我添加了调试代码而我没有在weiner中看到日志。

我有一些全局配置的跟随模型:

// The contents of individual model .js files will be concatenated into dist/models.js

(function() {

// Protects views where angular is not loaded from errors
if ( typeof angular == 'undefined' ) {
    return;
};


var module = angular.module('Provedores_floresModel', ['restangular']);

module.factory('Provedores_floresRestangular', function(Restangular) {

  return Restangular.withConfig(function(RestangularConfigurer) {

    RestangularConfigurer.setBaseUrl('https://public-api.wordpress.com/rest/v1/sites/www.bride2be.com.mx/');
    RestangularConfigurer.setResponseInterceptor(function(response, operation, what, url){

        var newResponse;

        if (operation === "getList") {
            newResponse = response.posts;
        }

        return newResponse;
    });

  });

});


})();

和这个控制器:

var provedores_floresApp = angular.module('provedores_floresApp', ['Provedores_floresModel', 'hmTouchevents']);


// Index: http://localhost/views/provedores_flores/index.html

provedores_floresApp.controller('IndexCtrl', function ($scope, Provedores_floresRestangular) {

  // Helper function for opening new webviews
  $scope.open = function(id) {
    webView = new steroids.views.WebView("/views/provedores_flores/show.html?id="+id);
    steroids.layers.push(webView);
  };

  // Fetch all objects from the local JSON (see app/models/provedores_flores.js)
  $scope.provedores_floress = Provedores_floresRestangular.all('posts?type=ceremonia').getList().then(function() {
    alert("All ok");
  }, function(response) {
    alert(JSON.stringify(response));
  });
  // -- Native navigation
  steroids.view.navigationBar.show("Flores");

});

在当时的代码块上我收到了以下回复:

{
    "data": "",
    "status": 0,
    "config": {
        "method": "GET",
        "headers": {},
        "url": "https://public-api.wordpress.com/rest/v1/sites/www.bride2be.com.mx/posts?type=ceremonia"
    }
}

1 个答案:

答案 0 :(得分:0)

问题是CORS,所以我将以下代码添加到我的.htaccess

RewriteEngine On                  
RewriteCond %{REQUEST_METHOD} OPTIONS 
RewriteRule ^(.*)$ $1 [R=200,L]

<ifModule mod_headers.c>
    Header always set Access-Control-Allow-Origin: "*"
    Header always set Access-Control-Allow-Methods "POST, GET, PUT, DELETE, OPTIONS"
    Header always set Access-Control-Allow-Headers "X-Requested-With"
</ifModule>