>我在Ionic应用程序中遇到问题。Rest api是用Scala编写的,它在浏览器和邮递员上的网站和Ionic应用程序中正常工作。但它不适用于应用程序。当我点击该 API 时,我面临403 问题。 我试图更新 scala 配置文件
cors {
pathPrefixes = ["/api"]
}
现在它在应用程序中工作正常,但在浏览器中无法正常工作,该怎么办?是来自 API 端还是 Ionic 代码端的问题?我的代码如下
let params = {"vendor._id":"1"};
let headers = new Headers({
"Content-Type": "application/json"
});
let options = new RequestOptions({
headers: headers
});
this.http.post('apiurl',params, options)
这是科尔多瓦的Play框架问题。以下链接将对此进行解释:
https://forum.ionicframework.com/t/ionic-http-request-with-403-error-on-ipad-device/50269/3
原因:
科尔多瓦通过标头Origin: file://…
发送请求
您可以查看此链接以处理该问题 玩!2.4: 如何允许来自源 file://的 CORS
删除从 Play 框架导入的默认课程过滤器并编写基于条件的过滤器:
import play.api.Logger
import play.api.libs.concurrent.Execution.Implicits.defaultContext
import play.api.mvc._
import play.mvc.Http
/**
* Allow CORS from anywhere, any method
*/
class CORSFilter extends EssentialFilter {
def apply(nextFilter: EssentialAction) = new EssentialAction {
def apply(requestHeader: RequestHeader) = {
nextFilter(requestHeader)
.map { result =>
if (requestHeader.method.equals("OPTIONS")) {
Results.Ok.withHeaders(
Http.HeaderNames.ACCESS_CONTROL_ALLOW_ORIGIN -> "*",
Http.HeaderNames.ACCESS_CONTROL_ALLOW_HEADERS -> "Access-Control-Allow-Origin,X-Requested-With, Accept, Content-Type,application, idUser, access-control-allow-methods, token, access-control-allow-credentials, Authorization",
Http.HeaderNames.ACCESS_CONTROL_ALLOW_METHODS -> "HEAD,GET,POST,PUT,PATCH,DELETE")
} else {
result.withHeaders(
Http.HeaderNames.ACCESS_CONTROL_ALLOW_ORIGIN -> "*",
Http.HeaderNames.ACCESS_CONTROL_ALLOW_HEADERS -> "X-Requested-With, Accept, Content-Type",
Http.HeaderNames.ACCESS_CONTROL_ALLOW_METHODS -> "HEAD,GET,POST,PUT,PATCH,DELETE",
Http.HeaderNames.ACCESS_CONTROL_EXPOSE_HEADERS -> "X-Custom-Header-To-Expose")
}
}
}
}
}