0

我一直在苦苦挣扎Access-Allow-Control-Origin,我正在使用 Divshot。在我的移动应用程序中,我正在显示 WordPress 帐户的帖子,当我在浏览器中测试我的应用程序时,我可以看到这些帖子,但是一旦我在 Chrome for mobile 中打开该应用程序,我就无法看到这些帖子。根据我看到的,有些人有同样的问题,所以我需要在 Divshot 配置文件中使用 CORS。

在 Divshot 页面中,这就是他们所说的,但我不知道如何在我的应用程序中执行此操作

Custom Headers
If you need to set custom response headers for specific routes,
you can use the headers key in your configuration file:

{
  "headers": {
    "/cors-stuff/**": {
      "Access-Control-Allow-Origin": "*"
    },
    "/scripts/**": {
      "content-type": "text/javascript"
    }
  }
}

This can be useful for applying a content security policy, enforcing a different content-type, enabling cross-origin resource sharing (CORS), and more.

这就是我的配置文件中的内容

{
  "name": "urbanetradio",
  "root": "www",
  "clean_urls": true,
  "error_page": "error.html",
  "headers": {
    "Access-Control-Allow-Origin": "*",
    "/js/**": {
      "content-type": "text/javascript"
    }
  }
}

但我仍然没有任何好的结果。

4

1 回答 1

0

以防万一有人想知道,在执行请求时,您必须发送更多参数,请查看示例,我现在可以使用

getBlogs: function($scope) {
  $scope.postsURL = 'http://urbanetradio.com/wp-json/posts?_jsonp=JSON_CALLBACK';
  $http.jsonp($scope.postsURL).success(function(data, status, headers, config) {
    console.log(config);
    $scope.posts = data;
  }).error(function(data, status_headers, config) {
      console.log('error');
  });
}
于 2015-05-18T02:57:18.977 回答