CORS预检频道未成功执行nextcloud和angular 6

时间:2018-07-31 11:46:34

标签: apache cors nextcloud

我使用angular 6作为前端ui,并在apache之上使用nextcloud。

我尝试使用Provisioning API nextcloud创建用户。 以下是我在角度6上的服务:

const httpOptions = {
headers: new HttpHeaders({
  'Content-Type': 'application/json',
  'Authorization':'Basic YWRtaW46UGlqZXBpamUxPw==',
  'OCS-APIRequest':'true '
})
};

@Injectable({ providedIn: 'root' })
export class NextcloudService {

private ncUrl = 'http://localhost/nextcloud/ocs/v1.php/cloud/users';

constructor(
private http: HttpClient,
private _token: TokenService,
private _error: ErrorHandlerService
) { }

addNewUser (): Observable<User> {
   return this.http.post(this.ncUrl, JSON.stringify({ "userid": "user1", "password": "user1234!@#" }), httpOptions).pipe(
  tap(),
  catchError(this._error.handleErrorStatus<any>())
);
}

}

但根据要求将出现此错误。

Cross-Origin Request Blocked: The Same Origin Policy disallows reading the remote resource at http://localhost/nextcloud/ocs/v1.php/cloud/users. (Reason: CORS header ‘Access-Control-Allow-Origin’ missing).
Cross-Origin Request Blocked: The Same Origin Policy disallows reading the remote resource at http://localhost/nextcloud/ocs/v1.php/cloud/users. (Reason: CORS request did not succeed).

所以我在apache上添加了一些配置:

Alias /nextcloud "/var/www/nextcloud/"

<Directory /var/www/nextcloud/>
  Options +FollowSymlinks
  AllowOverride All

 <IfModule mod_dav.c>
  Dav off
 </IfModule>

 Header always set Access-Control-Allow-Origin "*"
 Header always set Access-Control-Allow-Methods "POST, GET, OPTIONS, DELETE, PUT"
 Header always set Access-Control-Max-Age "1000"
 Header always set Access-Control-Allow-Headers "x-requested-with, Content-Type, origin, authorization, accept, client-security-token"

 # Added a rewrite to respond with a 200 SUCCESS on every OPTIONS request.
 RewriteEngine On
 RewriteCond %{REQUEST_METHOD} OPTIONS
 RewriteRule ^(.*)$ $1 [R=200,L]


 SetEnv HOME /var/www/nextcloud
 SetEnv HTTP_HOME /var/www/nextcloud

</Directory>

重新启动apache后,将出现此错误。我在这里停留了几天。

Cross-Origin Request Blocked: The Same Origin Policy disallows reading the remote resource at http://localhost/nextcloud/ocs/v1.php/cloud/users. (Reason: CORS preflight channel did not succeed).
Cross-Origin Request Blocked: The Same Origin Policy disallows reading the remote resource at http://localhost/nextcloud/ocs/v1.php/cloud/users. (Reason: CORS request did not succeed).

我不确定我是否应该更改nextcloud应用程序本身的代码。从理论上讲,我认为我们可以在服务器端进行更改。 我也确实更改了这些选项的CORS,但仍然相同:

'PUT, POST, GET, DELETE, PATCH',
'Authorization, Content-Type, Accept',
'1728000'

0 个答案:

没有答案