0

大家好,我正在开发一个应用程序,其后端是用 Wordpress 制作的,带有一个 rest 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");

});

在 then 代码块上,我得到了以下响应:

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

1 回答 1

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>
于 2013-10-30T00:04:41.293 回答